-1

I'm using jQuery Countdown plugin to implement a Countdown and call a webservice when timer expires.

The problem is that I'm using AJAX on the page, and have to re-setup the Countdown on every AJAX request like so:

var prm = Sys.WebForms.PageRequestManager.getInstance();

prm.add_endRequest(SetupTimer);

/*Initial setup*/
$(document).ready(function() {
    SetupTimer();
});

function SetupTimer() {
        var serverTime = new Date();
        var cutoffTime = new Date($("#<%= cutoffTime.ClientID %>").val());
        serverTime.setHours(serverTime.getHours());
        if (serverTime < cutoffTime) {
            $('#timer').countdown('destroy'); /*should work, but doesn't*/
            $('#timer').countdown({ until: cutoffTime, serverTime: serverTime, onExpiry: OrderingEnded, format: 'yowdHMS' });
        } 
        else
            OrderingEnded();
    }

This, for some reason, creates a new instance of the Countdown on ever request, resulting in numerous calls to Webservice when Countdown expires.

How do I make sure that only one instance of the Countdown exists at a time?

EDIT

found this in documentation, doesn't do it for me though

        $('#timer').countdown('destroy');
Community
  • 1
  • 1
roman m
  • 26,012
  • 31
  • 101
  • 133

3 Answers3

1

ok, figured it out, just needed to call the

$('#timer').countdown('destroy');

on beginRequest

var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(DeleteTimer);
prm.add_endRequest(SetupTimer);

$(document).ready(function() {
    SetupTimer();
});


function DeleteTimer() {
    $('#timer').countdown('destroy');
}
function SetupTimer() {
    var serverTime = new Date();
    var cutoffTime = new Date($("#<%= cutoffTime.ClientID %>").val());
    serverTime.setHours(serverTime.getHours());
    if (serverTime < cutoffTime) {          
        $('#timer').countdown({ until: cutoffTime, serverTime: serverTime, onExpiry: OrderingEnded, format: 'yowdHMS' });
    } 
    else
        OrderingEnded();
}
Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
roman m
  • 26,012
  • 31
  • 101
  • 133
0

I have a similar type of problem. I am using web services to get the current time from the server using serversync function in jquery.countdown.js plugin. I have a facing a problem

How countdown get Synchronise with jquery using "jquery.countdown.js" plugin?

will you please find the solution and let me know thanks.

Community
  • 1
  • 1
Basant B. Pandey
  • 345
  • 7
  • 22
0

You probably want setInterval(). It allows you to repeat the timer every X milliseconds. You can use clearInterval() to stop it.

Here is some reading comparing setInterval() and setTimeout().

Another option to only recreate the timer in the callback function of the AJAX call. This will ensure a new timer is only started once the previous AJAX call is complete, such as:

 function CheckServer() {
     $.get('/get_something.html', function() {
         timer = setTimeout('CheckServer', 5000);
     });
 }
Darryl Hein
  • 142,451
  • 95
  • 218
  • 261