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

javascript - js setInterval is working but setTimeout isn't - Stack Overflow

programmeradmin2浏览0评论

I'm learning about setTimeout vs setInterval and I have got setInterval to work but not setTimeout. for example this doesnt work:

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Brewery</title>
        <script type="text/javascript" src=".6.1/jquery.min.js"></script>
        <script type="text/javascript"> 
        function doNothing()
        {
            var t = "hello wolrd";
        }

        function poll(){
        $.ajax({
            //url: "http://192.168.0.11/"+Math.random(),
            url:"line-ajax.htm",
            contentType:"text/html",
            success: 'doNothing()'
        });
        }

        setTimeout(poll(),2000);
        </script>
        <link rel="stylesheet" type="text/css" href=".css" />
    </head>
    <body>
        <div id="container" style="width: 1200px; height: 500px; margin: 0 auto"></div>             
    </body>
</html>

but if i change only this

setTimeout(poll(),2000);

to

setInterval(poll(),2000);

it will make an ajax request every 2 seconds...

what is going on?

---edit i have also tried setTimeout(poll,2000); but that doesn't make the ajax request every 2 sec either.

I'm learning about setTimeout vs setInterval and I have got setInterval to work but not setTimeout. for example this doesnt work:

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Brewery</title>
        <script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.6.1/jquery.min.js"></script>
        <script type="text/javascript"> 
        function doNothing()
        {
            var t = "hello wolrd";
        }

        function poll(){
        $.ajax({
            //url: "http://192.168.0.11/"+Math.random(),
            url:"line-ajax.htm",
            contentType:"text/html",
            success: 'doNothing()'
        });
        }

        setTimeout(poll(),2000);
        </script>
        <link rel="stylesheet" type="text/css" href="http://www.highcharts./highslide/highslide.css" />
    </head>
    <body>
        <div id="container" style="width: 1200px; height: 500px; margin: 0 auto"></div>             
    </body>
</html>

but if i change only this

setTimeout(poll(),2000);

to

setInterval(poll(),2000);

it will make an ajax request every 2 seconds...

what is going on?

---edit i have also tried setTimeout(poll,2000); but that doesn't make the ajax request every 2 sec either.

Share Improve this question edited Jan 24, 2012 at 3:34 cobolstinks asked Jan 24, 2012 at 3:06 cobolstinkscobolstinks 7,15318 gold badges73 silver badges103 bronze badges 2
  • What do you mean by "doesn't work"? Do you get an error message, or...? With your code as shown, both setTimeout(poll(),2000) and setInterval(poll(),2000) should make the ajax request exactly once, and make it immediately, because you are calling the poll() function immediately and passing its return (undefined) to setTimeout() or setInterval(). – nnnnnn Commented Jan 24, 2012 at 3:31
  • i should have been more specific with what I mean by doesn't work sorry. I'm trying to get setTimeout to call the 'poll' function every 2 seconds, it is not. But set setInterval(poll(),2000); does call the poll function every 2 secs. Im trying to understand why. Thank you. – cobolstinks Commented Jan 24, 2012 at 3:37
Add a ment  | 

4 Answers 4

Reset to default 4

Remove the () inside the setTimeout or setInterval. Treat the function name as a variable, and that's what you're passing.

"I'm trying to get setTimeout to call the 'poll' function every 2 seconds, it is not"

The .setTimeout() method calls the function you pass it exactly once, after the specified delay.

The .setInterval() method calls the function you pass it repeatedly, with the specified delay between each call.

From the update to your question you seem to think they both do the same thing. Please read the doco I've linked to.

Note that you have to pass a function reference (or a string to be eval'd), so say:

setInterval(poll, 2000);
// NOT
setInterval(poll(), 2000);

The latter should not work because it calls poll() immediately and passes its return value (undefined) to setInterval(), so I really can't understand why it worked for you.

As the parameter of setTimeout is eval'd you should try this:

setTimeout("poll()",2000);

Or if you like to work with anonymous functions better than with strings that get eval'd:

setTimeout(function() {
  poll();
},2000);

I personally like the latter more.

The first argument to setTimeout() should be a reference to the function to be called, not a function call with (). So try:

setTimeout(poll, 2000);
//-----------^^^
// No parentheses...

Likewise, in the $.ajax() call, the success function should be a function pointer, not a string:

$.ajax({
        //url: "http://192.168.0.11/"+Math.random(),
        url:"line-ajax.htm",
        contentType:"text/html",
        // Function pointer to doNothing
        success: doNothing
  });
发布评论

评论列表(0)

  1. 暂无评论