zangw
zangw

Reputation: 48526

setInterval or setTimeout with String in JavaScript

Now I test the following codes about setInterval and setTimeout with String as parameter.

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

In IE, the output is 2214, I want to know why the result is 2214?

But test it in Chrome, the output is

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

According to the syntax of 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

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

I am not clear what happened about those codes?

Upvotes: 0

Views: 197

Answers (1)

James Westman
James Westman

Reputation: 2690

setInterval() and setTimeout() return timer IDs. 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(). The return value of the code, AFAIK, will be thrown away unless you put it somewhere. As Thilo said in the comments, the code will not even have been run by the time setInterval() and setTimeout() returns.

Upvotes: 1

Related Questions