Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7128031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:05:44+00:00 2026-05-28T11:05:44+00:00

I’m learning about setTimeout vs setInterval and I have got setInterval to work but

  • 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="http://ajax.googleapis.com/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.com/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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T11:05:45+00:00Added an answer on May 28, 2026 at 11:05 am

    “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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
i got an object with contents of html markup in it, for example: string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.