定时器有间隔警报

定时器有间隔警报

本文介绍了定时器有间隔警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有一个jquery计时器,从10倒数到0。在5秒钟我想显示一条警告信息然后在0秒我想显示另一条消息。



我使用下面的代码在表单字段中执行倒计时以及提供警报,但是当警报显示时倒计时停止:

  $(function(){
var count = 10;
countdown = setInterval(function(){
$(#Exam_Timer)。val(计数+秒剩余!);
if(count == 5){
alert('仅剩5秒');
}
if(count == 0){
alert('Times Up')
}
count--;
},1000);
});

有人可以让我知道如何重组这个以便警报不会停止倒计时吗? / p>

我尝试将警报放入单独的功能但这没有用。我创建了一个小提琴:



解决方案

你不能用 alert 因为它停止脚本执行。但是,您可以使用jQuery UI对话框实现此目的。请查看以下演示



工作演示


I have a jquery timer on my page that counts down from 10 to 0. At 5 seconds I want to show a warning message then at 0 seconds I want to show another message.

I have used the code below which will do the count down in the form field as well as give the alerts, however the countdown stops when the alerts show:

$(function(){
    var count = 10;
    countdown = setInterval(function(){
        $("#Exam_Timer").val(count + " seconds remaining!");
        if (count == 5) {
            alert('Only 5 Seconds Left');
        }
        if (count == 0) {
            alert('Times Up')
        }
    count--;
    }, 1000);
});

Can someone let me know how I would restructure this so that the alerts do no stop the countdown?

I tried putting the alert into a separate function but that did not help. I have created a Fiddle:

http://jsfiddle.net/CQu7T/

解决方案

You cant do this with alert as it stop scripts execution. However you can achieve this with jQuery UI dialogs. Check the following demo

Working Demo

这篇关于定时器有间隔警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:14