admin管理员组

文章数量:1391987

I'm going thru some legacy code and I've noticed the following line(s):

// Stray timeout
var t = setTimeout('',1);

Does it do anything? Is it really required?


There is a following question on SO: Why is setTimeout(fn, 0) sometimes useful?

The solution is to "pause" the JavaScript execution to let the rendering threads catch up. And this is the effect that setTimeout() with a timeout of 0 does. It is like a thread/process yield in C. Although it seems to say "run this immediately" it actually gives the browser a chance to finish doing some none-JavaScript things that have been waiting to finish before attending to this new piece of JavaScript.

That explains benefits of executing a function in setTimeout. But how about executing an empty string?

I'm going thru some legacy code and I've noticed the following line(s):

// Stray timeout
var t = setTimeout('',1);

Does it do anything? Is it really required?


There is a following question on SO: Why is setTimeout(fn, 0) sometimes useful?

The solution is to "pause" the JavaScript execution to let the rendering threads catch up. And this is the effect that setTimeout() with a timeout of 0 does. It is like a thread/process yield in C. Although it seems to say "run this immediately" it actually gives the browser a chance to finish doing some none-JavaScript things that have been waiting to finish before attending to this new piece of JavaScript.

That explains benefits of executing a function in setTimeout. But how about executing an empty string?

Share Improve this question edited May 23, 2017 at 10:33 CommunityBot 11 silver badge asked May 12, 2014 at 18:33 Mars RobertsonMars Robertson 13.3k13 gold badges72 silver badges93 bronze badges 6
  • GitHub flavored markdown doesn't work here. You can't use the 3 backticks to indent code. – gen_Eric Commented May 12, 2014 at 18:35
  • 2 Is t used anywhere below? – Bergi Commented May 12, 2014 at 18:37
  • @Bergi: Even if it was, this still wouldn't do anything? Would it? – gen_Eric Commented May 12, 2014 at 18:41
  • @RocketHazmat: Some (most?) browsers use auto-incrementing timeout ids. You might do while(t--) clearTimeout(t); to kill all outstanding ones. – Bergi Commented May 12, 2014 at 20:42
  • 1 @RocketHazmat: Yeah. Though, as Matt has laid out in his answer, it wouldn't even work. – Bergi Commented May 12, 2014 at 20:54
 |  Show 1 more ment

2 Answers 2

Reset to default 6
setTimeout('',1)

This line does absolutely nothing. You are telling JavaScript to eval() the string ''. That does nothing.

Sometimes doing this is useful, as it "pushes the code to the bottom of the stack" to run it after the function (and whatever UI updates the browser is doing) finishes.

For example:

setTimeout(function(){
    // Work with some DOM elements, after the browser has a second to render them
}, 0);

But I can't see an empty string having any effect on anything.

EDIT: In Google Chrome, if you do setTimeout('',1) it doesn't return you anything. Normally, it would return you the timeout_id, so you could cancel it with clearTimeout. But, in Google Chrome, setTimeout('',1) returns undefined, which makes be believe it doesn't even set a timeout, since you didn't give it anything to do. So, yeah, it literally does absolutely nothing.

One possibility is that it is a browser version test.

For example, in IE9,

setTimeout('',1)

returns undefined. However in IE10, it returns an integer. As Rocket Hazmat pointed out, which I also confirmed, Chrome 33 returns undefined.

本文标签: settimeoutquotStray timeoutquotwhat does the empty timeout in javascript doStack Overflow