在JavaScript中显示警报时如何保持计时器运行 [英] How to keep timer running when alert is displayed in javascript

查看:99
本文介绍了在JavaScript中显示警报时如何保持计时器运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示计时器的Javascript函数。
现在,当计时器达到2分钟时,我想显示一个警报并继续我的计时器功能。
但是,我的计时器功能停止,直到用户单击确定,然后从1:59秒恢复。

I am having a Javascript function which displays timer. Now,when timer reaches 2 minutes, I want to display a alert and continue my timer function. But,my timer function stops till user clicks 'Ok' and then resumes from 1:59 secs.

有人可以告诉我如何保持计时器功能吗?

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();

这是我的警报功能。

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


推荐答案

警告是阻止程序,请使用自定义的javascript弹出窗口,例如lightbox,lytebox,jquery对话框,fancybox等。

Alert is a blocker, use custom javascript popups like lightbox,lytebox,jquery dialog,fancybox etc.

或者您也可以简单地显示/隐藏浮动div。这样可以解决计时器
卡住的问题,并增强用户体验。

Or you can simply show/hide a floating div. This will solve the problem of your timer getting stuck, and also enhance your user experience.

这篇关于在JavaScript中显示警报时如何保持计时器运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆