Can setTimeout ever return 0 as the id?

前端 未结 4 538
心在旅途
心在旅途 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:47

    The specifications from Microsoft, Sun and Mozilla just say that it will return an integer. So 0 may be included.

    It may be possible (and probable) that some implementations exclude 0 but you shouldn't rely on that. You should go with the !==.

    To summarize: Although probably all browser exclude 0 from the IDs returned by setTimeout, you should not write your code with that in mind especially when all your have to do is add an extra =.

提交回复
热议问题