如何使用jquery问题重新启动javascript函数
您好,我有一个通过php/html执行的计时器,如下所示如何使用jquery问题重新启动javascript函数,javascript,jquery,html,Javascript,Jquery,Html,您好,我有一个通过php/html执行的计时器,如下所示 // timer print "<script>countdown();</script>"; //计时器 打印“倒计时();”; 然而,我有一个jquery警告对话框,看起来像这样,我希望javascript函数在单击关闭按钮后重新启动 // Create timeout warning dialog $('body').append('<div title="Timeout"</
// timer
print "<script>countdown();</script>";
//计时器
打印“倒计时();”;
然而,我有一个jquery警告对话框,看起来像这样,我希望javascript函数在单击关闭按钮后重新启动
// Create timeout warning dialog
$('body').append('<div title="Timeout"</div>');
$('#sessionTimeout-dialog').dialog({
autoOpen: false,
width: 600,
modal: true,
closeOnEscape: false,
open: function(event, ui) { $(".dialog").hide(); },
buttons: {
// Button two - closes dialog and makes call to keep-alive URL
"Continue Session": function() {
$(this).dialog('close');
clearTime();
$.ajax({
type: 'POST',
url: o.keepAliveUrl
});
countdown(); //I call the javascript function in hopes of making it go again
}
}
});
//创建超时警告对话框
$('body').append('看起来倒计时函数不会重置计时器,而只是启动递减循环
添加如下内容
function resetCountdown()
{
secs = mins * 2;
}
并在对话框关闭时调用此函数以重置倒计时。不要再次调用countdown(),因为这将在已运行的计时器之外创建另一个计时器
您的代码中还有一个错误:
if (seconds < 59) {
seconds.value = secs;
} else {
minutes.value = getminutes();
seconds.value = getseconds();
}
if(秒<59){
秒。值=秒;
}否则{
minutes.value=getminutes();
seconds.value=getseconds();
}
应该是
if (secs < 59) {
minutes.value = 0;
seconds.value = secs;
} else {
minutes.value = getminutes();
seconds.value = getseconds();
}
if(秒<59){
分钟。数值=0;
秒。值=秒;
}否则{
minutes.value=getminutes();
seconds.value=getseconds();
}
我不完全确定这是您想要的,但是:
我喜欢在窗口(从对话框)恢复焦点时重新启动某些计时器。下面是一个示例:
<html>
<head>
<script>
g = {};
g.timer = false;
g.count = false;
g.waittime = 3; //seconds for timer.
g.timeleft = (g.waittime * 100) - 10;
checktimer = function(){
if(g.timer === false){
g.timer = setTimeout(function(){
g.timer = false;
g.timeleft = g.waittime * 100;
alert("Hi!");
},g.waittime * 1000);
}
if(g.count === false){
g.count = setInterval(function(){
g.timeleft-=10;
document.getElementById("disp").innerText =(g.timeleft/100).toString().match(/^\d+(?:\.\d{0,2})?/);
},100);
}
}
window.onfocus = function() {
checktimer();
};
</script>
</head>
<body onload="checktimer();">
<div>Time Left: <span id="disp">0</span> Seconds.</div>
</body>
</html>
g={};
g、 定时器=假;
g、 计数=假;
g、 waittime=3;//计时器的秒数。
g、 timeleft=(g.waittime*100)-10;
checktimer=函数(){
如果(g.timer===false){
g、 计时器=设置超时(函数(){
g、 定时器=假;
g、 timeleft=g.waittime*100;
警惕(“嗨!”);
},g.等待时间*1000);
}
如果(g.count==false){
g、 计数=设置间隔(函数(){
g、 时限-=10;
document.getElementById(“disp”).innerText=(g.timeleft/100).toString().match(/^\d+(?:\。\d{0,2})/);
},100);
}
}
window.onfocus=函数(){
checktimer();
};
剩余时间:0秒。
什么是controlreditermer
?@Blender这是一个函数尝试,我删除了它,因为它不再相关了,很抱歉混淆了
<html>
<head>
<script>
g = {};
g.timer = false;
g.count = false;
g.waittime = 3; //seconds for timer.
g.timeleft = (g.waittime * 100) - 10;
checktimer = function(){
if(g.timer === false){
g.timer = setTimeout(function(){
g.timer = false;
g.timeleft = g.waittime * 100;
alert("Hi!");
},g.waittime * 1000);
}
if(g.count === false){
g.count = setInterval(function(){
g.timeleft-=10;
document.getElementById("disp").innerText =(g.timeleft/100).toString().match(/^\d+(?:\.\d{0,2})?/);
},100);
}
}
window.onfocus = function() {
checktimer();
};
</script>
</head>
<body onload="checktimer();">
<div>Time Left: <span id="disp">0</span> Seconds.</div>
</body>
</html>