Can setTimeout ever return 0 as the id?

前端 未结 4 518
心在旅途
心在旅途 2021-01-03 17:48

I am writing a check to see if a timeout is active. I was thinking of doing this:

var a = setTimeout(fn, 10);
// ... Other code ... where clearTimeout(a) can         


        
4条回答
  •  别那么骄傲
    2021-01-03 18:32

    First: 0 isn't the same as null, (0 == null) would be false in every case';

    if you want to test 'a' against something: define 'a' first and later assign the settimeout to 'a'. then check against the type of 'a'. If its 'undefined', the timer hasn't triggered yet

提交回复
热议问题