最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is this browser-dependent javascript code? - Stack Overflow

programmeradmin2浏览0评论

Why different output in IE and FF?
In IE its showing : Hello and In FF its showing : Hi

var message = "Hi";
setTimeout(function(){alert(message);},10);
setTimeout(function(){message = "Hello";},0);

what is standarad? which browser is doing it right?

Note: if i convert 10 to 11 in FF then it shows Hello

Why different output in IE and FF?
In IE its showing : Hello and In FF its showing : Hi

var message = "Hi";
setTimeout(function(){alert(message);},10);
setTimeout(function(){message = "Hello";},0);

what is standarad? which browser is doing it right?

Note: if i convert 10 to 11 in FF then it shows Hello

Share edited Jul 28, 2010 at 6:29 coure2011 asked Jul 28, 2010 at 6:23 coure2011coure2011 42.6k87 gold badges225 silver badges361 bronze badges 1
  • This is probably more of a timing issue than anything else. Can you provide some information as to the actual application of this to whatever you're doing? – jtbandes Commented Jul 28, 2010 at 6:31
Add a ment  | 

4 Answers 4

Reset to default 6

Firefox handles small delays differently to IE. Firefox have a min delay time of 10ms (which isn't exact either). See the notes of https://developer.mozilla/en/window.setTimeout for more info.

On my PC I ran it in both FF and IE, and I had exactly the opposite results.

The reason for this is your timeout is just 10 milliseconds long. The resolution of Timers on Windows is actually about 10ms, so it's possible that either timeout could happen first. To be really sure that one thing happens before the other, you should definitely have a wider gap between timeouts.

And even then, you shouldn't expect it to always work :-)

If you really want to do things in the same order, keep it in the same line of code, or set flags saying whether or not a particular action has been pleted, and check that before doing a second one which relies on the first.

Order of execution for timer events is not guaranteed in browsers. They are handled internally by the operating system's native timer implementation and may fire in different order.

Since you have specified such a small amount of time, it's very likely that this is the case.

Well I clearly grasp it.

What firefox does is what you expect i.e :

  • Sets the first message after 1/100th of a second & caches the message value.
  • Shows the second message immediately.
  • Shows the first message {if 100 secs over}.

While what IE does is what brings the doubt :

  • Sets the message after 1/100th of a second.
  • Sets the message to new value, ignores new timeouts.
  • Shows the message {if 1/100 secs over}.

So Basically what I realize is that the message variable has a global scope in IE while firefox creates a cache of the value last passed to the timeout function. To realize this, use longer time periods possibly of 1000 & 10000 (1 & 10 sec) instead of 0 & 10. This will show you that firefox displays the alert twice while IE only once.

发布评论

评论列表(0)

  1. 暂无评论