jquery - pause timer until ajax completes -
i wrote following timer function check database every x seconds... pause timer until ajax completes , once complete start again.
currently, counts down 0 x seconds , when reaches 0 ajax check occurs. timer resets x seconds 0 reached , continues run until stopped.
i loading number of images webpage ajax may take time load these ... reason pause.
// start timer $('#dtstart').on('click', function () { if(timerstatus != true) { var timercount = $('#dtrefresh').val(); $("#dtstart").addclass('hide'); $("#dtstop").removeclass('hide'); } function countdown(count) { if(timerstatus != true) { timerstatus = true; timerid = setinterval(function() { count--; $("#dttimer").html(count); if(count == 0) { $("#dttimer").html(count); count = timercount; $.ajax({ type: 'post', url: '/process/p_realtime_screenshots.php', data: { ss_id : $("#slider .slides-content:eq("+(slider.count - 1)+")").data('id') }, datatype : 'json' }).done(function (response) { if (response.success) { //add new screenshots $.each( response.screenshots, function( index, value ) { //add slide slider.addslide('screenshot added here'); //correct count $(".slide-total-slides").text(" of "+slider.count); }); //stop playing slider if , auto go last slide added $('#slider').flexslider("stop"); $('#slider').flexslider(slider.count - 1); } }); } }, 1000); } } countdown(timercount); });
you can use clearinterval()
stop timer @ beginning of ajax request , start again on success
event:
$.ajax({ ... beforesend:function(){ clearinterval(timerid);} success : function(){ /* start timer again here */} ... });
Comments
Post a Comment