最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Which browsers (and versions) support callback arguments for setTimeout and setInterval? - Stack Overflow

matteradmin3PV0评论

According to WHATWG and MDN, window.setTimeout and window.setInterval have the form

var handle = window.setTimeout( handler [, timeout [, arguments... ] ] );
var handle = window.setInterval( handler [, timeout [, arguments... ] ] );

Most sources say (generic) Internet Explorer doesn't support the optional arguments.
Is there a list of browsers and versions that do and do not support it?

An example of the way to test for support would be

<html>
<head><title>test</title></head>
<body>
setTimeout: <span id="t">testing..</span><br/>
setInterval: <span id="i">testing..</span>
<script type="text/javascript">
t = window.setTimeout( // setTimeout
    function (b) { // callback
        document.getElementById('t').innerHTML = (b || false); // test for arg1
    },
    0,
    true // arg1
);
i = window.setInterval( // setInterval
    function (b) { // callback
        document.getElementById('i').innerHTML = (b || false); // test for arg1
        window.clearInterval(i);
    },
    0,
    true // arg1
);
</script>
</body>
</html>

With expected result true and result on failure as false.

According to WHATWG and MDN, window.setTimeout and window.setInterval have the form

var handle = window.setTimeout( handler [, timeout [, arguments... ] ] );
var handle = window.setInterval( handler [, timeout [, arguments... ] ] );

Most sources say (generic) Internet Explorer doesn't support the optional arguments.
Is there a list of browsers and versions that do and do not support it?

An example of the way to test for support would be

<html>
<head><title>test</title></head>
<body>
setTimeout: <span id="t">testing..</span><br/>
setInterval: <span id="i">testing..</span>
<script type="text/javascript">
t = window.setTimeout( // setTimeout
    function (b) { // callback
        document.getElementById('t').innerHTML = (b || false); // test for arg1
    },
    0,
    true // arg1
);
i = window.setInterval( // setInterval
    function (b) { // callback
        document.getElementById('i').innerHTML = (b || false); // test for arg1
        window.clearInterval(i);
    },
    0,
    true // arg1
);
</script>
</body>
</html>

With expected result true and result on failure as false.

Share Improve this question asked Sep 30, 2012 at 0:27 Paul S.Paul S. 66.4k9 gold badges128 silver badges143 bronze badges 2
  • 1 Since it is so easy to simply use an anonymous function to pass extra arguments and it runs everywhere, why not just use that and avoid the headaches? – Jeremy J Starcher Commented Sep 30, 2012 at 3:16
  • 1 It is a question I couldn't find an actual answer to anywhere, as in, the answers assumed every version of each browser is the same, so I went to find out and decided to share. – Paul S. Commented Sep 30, 2012 at 12:45
Add a ment  | 

1 Answer 1

Reset to default 8

Using a test based upon the example code in the question and BrowserShots for the browsers, here is a table of browser support

Browser  Version  setTimeout  setInterval
Chrome     4+      true        true         Lowest version testable
Firefox    3+      true        true         Did not test lower versions
MSIE       6       false       false
MSIE       7       false       false
MSIE       8       false       false
MSIE       9       false       false
MSIE      10       true        true
Opera                                       Not tested

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far