0

我需要有关 JavaScript 的帮助。我正在使用此代码来获取程序的执行时间:

if(seconds == sec){alert(sec);}

function countdown(element) {
    interval = setInterval(function() {
        var el = document.getElementById(element);    
        if(seconds == 0) {
          //alert(seconds);
          if(minutes == 0) {
            el.innerHTML = "00 : 00"; 
            alert("Sorry, you have exceeded our booking time and your order has been cancelled. We are afraid you have to start over!",null,'');
            clearInterval(interval);
            ajax_load_image();
            window.location.href='movies.html';
            return;
          } else {
            minutes--;
            seconds = 60;
          }
        }
        if(minutes > 0) {     
          var minute_text = minutes+(minutes > 1 ? ' ' : ' ');
        } else {
          var minute_text = '0';
        }
        if(minute_text<0)
        {
          $('#countdown').css('color','red');
        }
        var second_text =seconds > 1 ? '' : '';
        if(minute_text=='')
        {
          el.innerHTML ='0 : '+seconds;
        }
        else
        {
          if(seconds<10)
          {
            seconds='0'+seconds;
          }  
          el.innerHTML = '0'+minute_text + ' : ' + seconds + ' ' + second_text + ' Min';
        }
        seconds--;
    }, 1000);
}

当我锁定手机时,计时器会自动停止。当我锁定手机时,我该怎么做才能运行计时器。帮我。

4

1 回答 1

0

如果您关心经过的实际时间量,请不要依赖setInterval()- 将开始时间与当前时间进行比较。

var startTime = new Date();

setInterval(function() {
   // you might need to call .getTime() here - this works in Chrome,
   // but that might be implementation-dependent
   var seconds = ((new Date()) - startTime) / 1000;

   // etc
});

我不知道这将如何在移动环境中工作,但是当您解锁并setInterval()再次运行时,它应该会正确超时。

于 2012-08-04T06:32:07.533 回答