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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:42:19+00:00 2026-06-07T23:42:19+00:00

I am displaying a countdown watch with respect to a given endtime. although its

  • 0

I am displaying a countdown watch with respect to a given endtime.

although its working perfect but i want to know which is best methods to apply.

below is my countdown function.

  var timerId;
  var postData = {endDate : endDate, tz : tz};  
  var countdown = function()
    { 
      $.ajax({
               type : 'post',
               async : false,
               timeout : 1000,
               url : './ajax_countdown.php',
               data : $.param(postData),
               dataType : 'json',
               success : function (resp){
                  $('#currentTime').html(resp.remainingTime);
               }
            }); 
     }

what i want is that function (countdown) shoud be called automatically after every 1 second and if it does not execute/completed within 1 second then cancel the current ajax and start a new ajax call.

now I found there are 4 working methods

method 1: using setInterval() with window object

window.setInterval(countdown, 1000);

method 2 : using setInterval() independently

setInterval(function() {countdown()}, 1000);

method 3 : using setTimeOut inside the function an call other function to intialize main function

var countdown = function() { 
     $.ajax({ //ajax code });
     timerId = setTimeout(countdown, 5000); // assign to a variable
 }

function clockStart() {  
        if (timerId) return
        countdown();
}
clockStart(); // calling this function 

method 4 : using anonymous function call

var countdown = function() { 
     $.ajax({ //ajax code });
     timerId = setTimeout(countdown, 5000);
 }
  (function(){
         if (timerId) return;
         countdown();
})();

Please tell me

  • What is con and pro of each method and which one is best/right method?
  • Should i use clearTimeOut() or clearInterval() ?

References

http://javascript.info/tutorial/settimeout-setinterval

Calling a function every 60 seconds

http://www.electrictoolbox.com/using-settimeout-javascript/

  • 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-06-07T23:42:21+00:00Added an answer on June 7, 2026 at 11:42 pm

    The benefit of using #1 over #2 is that the window reference removes the chance of a scope variable overwriting setInterval.

    // When out of global scope...
    function setInterval() {
    
    }
    
    window.setInterval(foo, 100); // still calls the "correct" setInterval
    

    There’s no difference between wrapping the call to countdown in a function (#1, #2). #2 gives you greater flexibility as you can also call other functions/ pass arguments etc (although it’s obviously trivial to swap from #1 to #2 if this becomes the case).

    #4 saves you having to declare a function clockStart, other than that, it’s the same as #3.

    Use clearTimeout if you used setTimeout, and clearInterval if you used setInterval…

    You should also be aware of how setTimeout and setInterval work differently. There’s an amazing answer here which explains that…

    As for what I’d use? I’d use #2.

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

Sidebar

Related Questions

I am displaying tableview from custom cell and its working fine. But the problem
When displaying all articles of a specific month (given through the urls like this
The countdown is displaying as following HOUR & MIN & SEC. example 48Hours 12MIN
When displaying progress bars do you display generic messages, such as: Working Loading In
I am displaying a custom form as a hint. I want the hint window
Displaying content’s based on category and tags criteria works well but how should we
When displaying my DateTime values, I want them to be formatted like so: February
Before displaying user posts I run them though Sanitize::html() to escape all html. But
Now the wordpress is displaying the blog archives as following Janauary 2012 But I
I want to toggle displaying an image in my program. Here is my code

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.