Can setTimeout ever return 0 as the id?

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

    Most browsers will return an int starting at 1 and incrementing for each call of setTimeout so in theory it could never be 0.

    But keep in mind that this is not really a requirement of the spec, just a convention browsers tend to follow. See the accepted answer here for more details: setInterval/setTimeout return value

提交回复
热议问题