Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setTimeout - Weird behavior

Consider the following HTML:

<html>
    <head></head>
    <body>
        <input type="text" onblur="window.setTimeout('document.title += 2;', 0);" />
        <input type="button" onclick="document.title += 1" />
    </body>
</html>

[Demo with 0 delay, 100ms delay, 150ms delay]

And the following steps:

  • User enters the input (focus).
  • User clicks the button.

Now, the events would occur on the following order:

  • Input Text blur event.
  • Button click event.

Testing this on all reachable browsers I get:

document.title = '21' //Expected behavior

But! On production browser (Windows XP + IE 7), I get:

document.title = '12' //Unexpected behavior

I also tried simulating it in IE 7 mode on my local machine (IE 10), couldn't reproduce it tho.

This is obviously a simplified example of the problem I'm having. Otherwise I could simply get rid of the setTimeout.

In the real scenario, the setTimeout call is actually being made by a third party script library (ASP.NET Dev Express Components).

Apart from the actual solution to this problem (which I think I can handle), what explanation could be applied to this behavior?

Update:

Using the expression new Date().getTime() to get the time of each step executed by the browser. It happens as follows:

1387369361417 //document.title += 1
1387369361433 //document.title += 2
like image 745
Mateus Schneiders Avatar asked Dec 17 '13 16:12

Mateus Schneiders


1 Answers

Two possibilities:

  1. Your click (mousedown + mouseup) is finishing before IE7's minimum timeout period.
  2. The mousedown status is blocking scripts. Events must wait until other scripts and user-interactions are finished before they fire. And given the history of script-UI weirdness/terribleness in IE, I'd bet the mousedown "begins a user interaction" and the mouesup "ends user interaction". Load this up in IE7:

    <input type="text" onblur="window.setTimeout('output(2));', 0); output(3);" />
    <input type="button" onclick="output(1);" />
    

    http://jsfiddle.net/sMcE3/

    … and after you'd focus()'d on the text field, click that button real slow-like. I'm guessing you'll see 312. (As opposed to the 321 that any half-decent browser will show.)

like image 126
svidgen Avatar answered Sep 28 '22 00:09

svidgen