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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:53:41+00:00 2026-05-30T10:53:41+00:00

I have the following jQuery function that I’m using to display a timer on

  • 0

I have the following jQuery function that I’m using to display a timer on a page:

function update() {
  $.ajax({
    type: 'POST',
    url: 'check_time.php',
    data: 'checktime=true',
    timeout: 0,
    success: function(data) {
        $(".time_remaining").html(data);
        window.setTimeout(update, 1000);
        var time = data;
        if(time<=0)
        {
            $(".time_remaining").html("Reloading the page now.");
            refresh();
        }
        else
        {
            $(".time_remaining").html("There are "+data+" seconds left." );
        }
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      $("#notice_div").html('Error contacting server. Retrying in 60 seconds.');
      window.setTimeout(update, 60000);
    }
});
};

As you can see, it’s actually running a script that calculates how much time is remaining until a refresh is called (with the refresh() function). I feel this is a bit intensive because it’s calling every second, but I feel it’s important at the same time to have synchrony in the Ajax because if the refresh() function is called too early the page stops running in sync.

How can I make it that the timer is still always decreasing in time, but only synchronises with the server every 30 seconds or so?

Precision is really important for this application.

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

    Use a variable remainingTime to store the remaining time:

    var remainingTime;
    

    Update with ajax:

    function update() {
        $.ajax(..., success: function(data) {
            remainingTime = parseInt(data, 10);
        });
    }
    

    Continuously update:

    setInterval(update, 30 * 1000);
    

    Countdown:

    function countdown() {
        if(remainingTime-- < 0) {
            $(".time_remaining").text("Reloading the page now.");
            refresh();
        } else {
            $(".time_remaining").text("There are " + remainingTime + " seconds left." );
        }
    }
    

    Continuously countdown:

    setInterval(countdown, 1000);
    

    NOTE: It might be the case that you want to setTimeout inside the success handler, as you already did, and a longer timeout in the error handler. But this should do the trick for decoupling the updating from the display.

    You definitely should use setInterval for the countdown though, because setInterval tries to trigger with that exact interval, whereas setTimeout will cause drift, that is, if it takes 10ms to update the DOM, the next call will only occur after 1010ms, and so on. With setInterval, this is not the case because the browser will do its best to actually trigger that function every 1000 ms.

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

Sidebar

Related Questions

Let's say I have the following jQuery AJAX call: $.ajax({ type: POST, url: MyUrl,
I have the following jQuery function embedded in an MVC Razor page: <script type=text/javascript>
I have the following jquery function > <script type=text/javascript> > > $(document).ready(function() { >
i have the following Jquery function inside my view:- <script type=text/javascript> $(document).ready(function () {
i have the following jquery code: $(document).ready(function() { $('.restrictiveOptions input[type!=checkbox], .restrictiveOptions select').change(function() { //
I have the following JavaScript (I'm using jQuery): function language(language) { var text =
I have the following JQuery code that processes a GET request upon page load:
I have two list boxes and some JQuery function that does the following: $(document).ready(function
I have the following function that opens jQuery UI's dialog warning about the delete
I have the following jQuery AJAX request: function sendUpdate(urlToSend) { var code = AccessCode;

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.