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

  • SEARCH
  • Home
  • 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 6728475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:10:29+00:00 2026-05-26T10:10:29+00:00

So I know that there are differences between setTimeout and setInterval , but consider

  • 0

So I know that there are differences between setTimeout and setInterval, but consider these two code examples:

function myFunction(){
   setTimeout('myFunction();', 100);
   doSomething();
}
setTimeout('myFunction();', 100);

and

function myFunction(){
   doSomething();
}
setInterval('myFunction();', 100);

Note that in the first example I call setTimeout at the begining of the function and then I doSomething. Therefore there is no extra delay from doSomething(). Does that mean that those two examples do exactly the same? Or is there even more subtle difference?

  • 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-26T10:10:30+00:00Added an answer on May 26, 2026 at 10:10 am

    They’re functionally about the same, but there are differences. One difference is in how browsers handle it if doSomething takes longer than the interval. With setInterval, at least some browsers will just skip the next interval if doSomething is still running. So if you use 100ms as you have, and doSomething takes 110 ms to run, the next run won’t happen until 90ms later (of course, all of these times are approximate).

    Another difference is that with the setTimeout, you’ll get a new handle every time, whereas with setInterval you get one handle.

    Another difference with your examples as given is that in the setTimeout example, you’re firing up a JavaScript parser/compiler every time, whereas with setInterval you’re only firing up the parser/compiler once. But this difference shouldn’t matter, because you shouldn’t be doing that at all — see below.

    But subtleties aside, what you have there is functionally the same.


    Side note: It’s not best practice to pass strings into either setTimeout or setInterval. Instead, pass in a function reference:

    // setTimeout
    function myFunction(){
       setTimeout(myFunction, 100);
       doSomething();
    }
    setTimeout(myFunction, 100);
    
    // setInterval
    function myFunction(){
       doSomething();
    }
    setInterval(myFunction, 100);
    

    Passing in a string fires up a JavaScript parser and does the same thing as eval. It should be avoided whenever possible (and it’s almost always possible).

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

Sidebar

Related Questions

So, I know that there is a difference between these two tidbits of code:
I know that there are lots of examples out there on this, but they
I know the differences between the two . One notable thing is that abort()
I know that there're few differences between struct and class in C++. I also
I want to know if there are specific, standards-based differences between two different types
I know that there must be some differences. I have a Silverlight component (
I know that there is no way to fully protect our code. I also
I know that there can be multiple values for an email, but I'm not
I know that there are many Delphi database related questions available, but I'm considering
i know that there is some rules and standards in css handling but i

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.