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 1046357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:08:03+00:00 2026-05-16T16:08:03+00:00

In a javascript code I develop, some function should be called every 1 second.

  • 0

In a javascript code I develop, some function should be called every 1 second. But to be sure that this operation takes place every 1 second, the following code is utilized:

setInterval(doIt, 500);
function doIt() {
    var now = (new Date()).getTime();
    if(lastUpdate + 1000 >= now) {
       /// code...
       lastUpdate = now;
    }
}

As far as I know setInterval(doIt, 1000) doesn’t always mean that it’s called every one second.

Is the above solution is a valid one? If not, what do you recommend?

  • 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-16T16:08:04+00:00Added an answer on May 16, 2026 at 4:08 pm

    You could use setTimeout instead of setInterval, and make dynamic adjustments each time your function is called. The idea is to set the timeout for a number of milliseconds sufficient to carry you to the next second boundary.

    function timeoutFunc() {
      // do interesting things
      var time = new Date().getTime();
      setTimeout(timeoutFunc, 1000 - time % 1000);
    }
    

    You’d start it off with:

    setTimeout(timeoutFunc, 1000 - new Date().getTime() % 1000);
    

    Synchronizing with the server seems like a bad idea, because you have no way of knowing whether the client clock is synchronized to anything (like the NTP server network). If it’s not, then you server synchronizations are going to make things look wrong at the client, because the client clock will always be what seems right.

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

Sidebar

Related Questions

This Javascript code is using the 'this' keyword inside a nested function (which is
My javascript code is like this: function openWindows() { if(A is true) { window.open(URL_A);
This javascript code does not work in IE8, but works in Firefox and Google
Im looking into documenting the javascript code we develop and are looking for some
javascript code: function getCheckbox(name,id){ var check = 0; var deger = option-checkbox-+id; var dom
Javascript Code YAHOO.util.Event.onDOMReady(function (ev) { var carousel = new YAHOO.widget.Carousel(container, {isCircular: true, numVisible:1}); console.log(carousel.set('navigation',
My JavaScript code is as follows: $(document).keydown(function(event){ var move, inter; clearInterval(inter); inter = setInterval(move
The JavaScript code below is executed every time the user selects the login button
I am trying to understand how to develop stand-alone Javascript code. I want to
I'm helping a company develop a website that utilizes jquery but I have noticed

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.