简体   繁体   English

在JavaScript中使用String的setInterval或setTimeout

[英]setInterval or setTimeout with String in JavaScript

Now I test the following codes about setInterval and setTimeout with String as parameter. 现在,我以String作为参数测试有关setIntervalsetTimeout的以下代码。

>> setInterval(String,2) 
2214 
>> setInterval(String,2) 
2215 

In IE, the output is 2214 , I want to know why the result is 2214 ? 在IE中,输出为2214 ,我想知道为什么结果为2214

But test it in Chrome, the output is 但是在Chrome中测试它,输出是

setInterval(String, 2)
18
setInterval(String, 2)
19

According to the syntax of setInterval: 根据setInterval的语法:

setInterval(func|code, delay)

So I try the following 所以我尝试以下

>> String 
 function String() {     [native code] } 
>> String() 
"" 

There is no actual number result. 没有实际数字结果。

Also I try it with setTimeout 我也用setTimeout尝试

setInterval(String, 2)
20
setInterval(String, 2)
21
setTimeout(String, 2)
22

I am not clear what happened about those codes? 我不清楚这些代码发生了什么?

setInterval() and setTimeout() return timer IDs. setInterval()setTimeout()返回计时器ID。 These help the browser recognize them again if you clear them, but you don't need to worry about their specific values. 如果您清除它们,这些功能将帮助浏览器再次识别它们,但您不必担心它们的特定值。

The function/code that is run has nothing to do with the return value of setInterval() or setTimeout() . 运行的函数/代码与setInterval()setTimeout()的返回值无关。 The return value of the code, AFAIK, will be thrown away unless you put it somewhere. 除非将代码的返回值AFAIK放在某个位置,否则它将被丢弃。 As Thilo said in the comments, the code will not even have been run by the time setInterval() and setTimeout() returns. 正如Thilo在评论中所说,在setInterval()setTimeout()返回时,代码甚至都不会运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM