jeudi 9 avril 2015

javascript countdowntimer random ajax call executing multiple times

There are 30 boxes on the page and every box carries itemtime displaying random time in m:ss formate . Every second countdown() decrements the time to -1 seconds. when any itemtime reaches 0:00 time,new content is fetched by ajax call inside function loadbox().


PROBLEM:


Sometimes the ajax call is multiple times,thus updating the box in quick succession. I have logged calls in the getnewboxcontent.php, it is really multiple times with same data paraments. Tried many solutions already given on SOF.But still not solved. Not sure what I am missing... I tried like :


1.



loadBox(boxnum,function(output)
{
var boxName = 'boxplace'+boxnum;
$('#'+boxName).html(data);
});



  1. cache=false

  2. clearing and setting timer before and after the ajax call




Many thanks!!



`var countDownTimer = setInterval(function(){countDown()},1000);
function countDown(){
for(var i=1;i<=<?php echo $numberofboxes;?>;i++){
var timeBox = document.getElementById('itemtime'+i);
if (timeBox != null ) {
var lastTime = timeBox.innerHTML;
var timePart = lastTime.split(":");
var minutes = parseInt(timePart[0]);
var seconds = parseInt(timePart[1]);
if (minutes==0&&seconds==0){
loadBox(i);
}
else{
if (minutes>0&&seconds==0){minutes--;seconds=59}
else{seconds--;}
if (seconds < 10){seconds = "0"+seconds;}
timeBox.innerHTML = minutes+":"+seconds;
}
}
}
}
function loadBox(boxNumber){
var eTString = '#extratime'+boxNumber;
var extraTime = $(eTString).val();
var boxName = 'boxplace'+boxNumber;
var itemIDk=$('#item'+boxNumber).val();
$.get("functions/getnewboxcontent.php?boxNumber="+boxNumber+"&extraTime="+extraTime+"&reload=true"+"&itemnum="+itemIDk, function(data) {
var boxName = 'boxplace'+boxNumber;
document.getElementById(boxName).innerHTML = data;
console.log(boxNumber+' - '+itemIDk);
});
}
`




Aucun commentaire:

Enregistrer un commentaire