简体   繁体   English

在JavaScript中显示警报时如何保持计时器运行

[英]How to keep timer running when alert is displayed in javascript

I am having a Javascript function which displays timer. 我有一个显示计时器的Javascript函数。 Now,when timer reaches 2 minutes, I want to display a alert and continue my timer function. 现在,当计时器达到2分钟时,我想显示一个警报并继续我的计时器功能。 But,my timer function stops till user clicks 'Ok' and then resumes from 1:59 secs. 但是,我的计时器功能停止运行,直到用户单击“确定”,然后从1:59秒恢复。

Can anyone tell me how to keep the timer function running while popup box is displayed? 有人可以告诉我如何在显示弹出框时保持计时器功能运行吗?

Here's my function to display timer. 这是我显示计时器的功能。

var minutes=5;
var seconds=59;

function time_display(){
  if(minutes==2 && seconds==0){
    setTimeout('display_time_alert_two_minutes_left()',100);
  }
  if (seconds<=0){
    seconds=59;
    if(minutes>0)
      minutes-=1;
  }
  else
    seconds-=1;
  $('time_left_in_minutes').innerHTML = minutes+"."+ ljust_zero(seconds);
  setTimeout("time_display()",1000);
}
time_display();

and this is my alert function. 这是我的警报功能。

function display_time_alert_two_minutes_left(){
    alert('Two minutes left');
}

Alert is a blocker, use custom javascript popups like lightbox,lytebox,jquery dialog,fancybox etc. Alert是一个阻止程序,使用自定义javascript弹出窗口,例如lightbox,lytebox,jquery对话框,fancybox等。

Or you can simply show/hide a floating div. 或者,您可以简单地显示/隐藏浮动div。 This will solve the problem of your timer getting stuck, and also enhance your user experience. 这将解决计时器卡住的问题,并增强用户体验。

你如何使用那些没有那么麻烦的东西,像这样

As alert() stops the rest of the javascript code executing, it would be better to use something that doesn't require an imperitive style of incrementing time. 随着alert()停止其余javascript代码的执行,最好使用不需要强制性的时间增量形式的代码。

How about you actually use the time/date functionality, which will 'keep counting'? 您实际上如何使用将“继续计数”的时间/日期功能?

Edit : This will stop the web page updating when you call alert() but it will keep counting fine whilst an alert is open. 编辑:当您调用alert()时,这将停止网页更新,但是在打开警报时它将保持计数正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM