多次调用setInterval javascript函数

时间:2015-07-09 13:57:52

标签: javascript jquery timer setinterval

(见底部的小提琴手)我正在使用通话时间值。当呼叫到达队列时,我得到呼叫的到达时间,从那时起,我正在计算呼叫在队列中等待的时间。目标是显示队列中最长的等待呼叫。因此,如果我接到两个电话,首先我需要在计时器中显示第一个电话,当他们接听电话时,我需要为第二个电话的到达时间重置计时器并开始计时。 每当我在队列中得到一个新的呼叫,我得到它的到达时间然后我使用从那个时间开始的计时器。 我遇到的问题是我的函数被调用的次数与我的调用次数相同,这使得计时器同时增加了几秒钟

HandleResponseGroupeQueues: function (interactionQueueId, availableAgents, queueArrivalTime) {

    queueArrivalTime = parseInt(queueArrivalTime.substr(6)).toString();
    if (queueArrivalTime.startsWith('-')) {

        arrivalTimeField.text("00:00:00");
    }
    else {
       // debugger;
        var date = Math.round(new Date().getTime() / 1000.0); // current time in seconds
        queueArrivalTime = Math.round(new Date(parseInt(queueArrivalTime)).getTime() / 1000.0); // call arrival time in seconds
        var startTime = date - queueArrivalTime;
        var timeSinceCallCameInQueue = toHHMMSS(startTime);
        arrivalTimeField.text(timeSinceCallCameInQueue);
        setTime = setInterval(function () { waitingCallstimer(arrivalTimeField); }, 1000); //here is my timer
    }

    if (arrivalTimeField.text() === "00:00:00") {
        clearInterval(setTime);

我将每次调用serInterval()的时间转换成人类可读的时间。 计时器如下所示:

function waitingCallstimer(arrivalTimeField) {

var textTime = arrivalTimeField.text();
var  hours = textTime.substring(0, 2),
     minutes = textTime.substring(3, 5),
     seconds = textTime.substring(6, 8);

seconds = timeControl(parseInt(seconds) + 1);

if (seconds === 60) {
    seconds = '00';
    minutes = timeControl(parseInt(minutes) + 1);
    if (minutes === 60) {
        minutes = '00';
        hours = timeControl(parseInt(hours) + 1);
    }
}
timeControl(hours),
timeControl(minutes),
timeControl(seconds);
arrivalTimeField.text(hours + ':' + minutes + ':' + seconds);

如果我得到的数字低于10,则timeControl()仅用于添加0。 有没有办法解决这个问题或重做此代码以使其正确? thax!

http://jsfiddle.net/ye7on8Lq/ 看到这里!通过单击按钮,计时器将快速运行

1 个答案:

答案 0 :(得分:0)

更新:分叉JS小提琴并使用调用Array修复此问题 http://jsfiddle.net/o7sdc77z/

更新:使用JS Fiddle中的代码更新。这解决了这个问题,并且存在重大的重构。

基本理念

  • 每次添加呼叫ID时,请将其推送到数组callStartTimeArray
  • 每次完成呼叫(可以通过Curren call Done按钮模拟),该呼叫的条目将从该阵列中删除。而且 列表中的下一个用HandleResponseGroupeQueues
  • 处理
  • 不是从arrivalTimeField解析通话时长,而是使用currentCallTime变量来跟踪持续时间 当前的电话。

HTML

<span class="time_1"></span>
<input type="button" id="add-btn" value="Add Call" />
<input type="button" id="remove-btn" value=" Current Call Done" />

<强> JS

  var callStartTimeArray = [];
  var arrivalTimeField = $('.time_1');
  var setTime;
  var currentCallTime = 0;

  function HandleResponseGroupeQueues(queueId, availableAgents, queueArrivalTime) {
           if(setTime){
                clearInterval(setTime);
            }
           if(!queueArrivalTime){
              return ;
           }
            var date = Date.now(); // current time in milli seconds

            var startTime = Math.round((date - queueArrivalTime)/1000);
            var timeSinceCallCameInQueue = toHHMMSS(startTime);
            arrivalTimeField.text(timeSinceCallCameInQueue);

            setTime = setInterval(function () {      
                waitingCallstimer(arrivalTimeField); }, 1000);

    }

    function waitingCallstimer(arrivalTimeField) {
      currentCallTime++;

      arrivalTimeField.text(toHHMMSS(currentCallTime));

}

function timeToSeconds(time) {
    var parts = time.split(':');
    return (+parts[0]) * 60 * 60 + (+parts[1]) * 60 + (+parts[2]);
}

//converts seconds to total time
function toHHMMSS(sec) {
    var secNum = parseInt(sec, 10);
    var hours = timeControl(Math.floor(secNum / 3600)),
        minutes = timeControl(Math.floor((secNum - (hours * 3600)) / 60)),
        seconds = timeControl(secNum - (hours * 3600) - (minutes * 60));

    var time = hours + ':' + minutes + ':' + seconds;

    return time;
}




function timeControl(time) {
    if (time ==='00') {
       return time;
    }
    if (time < 10 ) {
        time = '0' + time;
    }
    return time;
}

$('#add-btn').click(function(){

    var currTime = Date.now();
    callStartTimeArray.push(currTime);
    if(callStartTimeArray.length == 1) {
        HandleResponseGroupeQueues(1, 2, currTime);
    }
    console.log('Added one more call---', callStartTimeArray);
});

$('#remove-btn').click(function(){
   callStartTimeArray.shift();
   //reset the text in textfield
   currentCallTime = 0;
   arrivalTimeField.text("00:00:00");

   //Handle the next call in Queue
   HandleResponseGroupeQueues(1, 2,callStartTimeArray[0]);
   console.log('After Last Call is done---', callStartTimeArray);
});