setTimeout返回什么? [英] What does setTimeout return?
问题描述
我很好奇setTimeout返回的是什么。所以我做了一个快速测试:
I was curious that what does setTimeout return. So I did a quick test:
var thing = setTimeout(function(){},1);
令我惊讶的是它给了我一个号码。 1351
每次都不同。
And what surprise me is that it gave me a number. 1351
Each time is different.
所以它真的只返回一个数字吗?所以我也可以这样做吗?
So is it really all it returns is a number? So I can actually do this as well?
clearTimeout(1351);
非常混乱......
Very confusing...
推荐答案
这是一个句柄(唯一标识符)。当您创建超时时,JavaScript运行时将句柄与您创建的超时相关联,并且它可以通过句柄 setTimeout()
返回来识别超时。当您运行 clearTimeout()
时,它会通过查看您传入的唯一句柄知道您正在谈论的超时。
It's a handle (a unique identifier). When you create a timeout, the JavaScript runtime associates a handle with the timeout you created, and it can identify that timeout by the handle setTimeout()
returns. When you run clearTimeout()
, it will know what timeout you're talking about by looking at the unique handle you pass in.
这篇关于setTimeout返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!