我正在使用 Jquery UI 对话框,如下所示:
var $dialog;
$(document).ready(function() {
$dialog = $('<div></div>')
.html('Are you awake?<br/><br/><button>Yes</button>')
.dialog({
autoOpen: false,
title: 'Inactivity Monitor'
});
});
此对话框每 20 分钟显示一次,用户必须单击"is"才能继续。我需要在这后面运行一些东西,这样如果用户在 10 分钟内没有单击"is",则会执行另一个功能(向总部发送电子邮件)。
我怎样才能做到这一点?
感谢您的帮助
最佳答案
您可以使用 setTimeout();
来执行该函数(如果达到),并且您可以将其设置为变量,以便在单击按钮时禁用计时器。
我已经设置了一个 jsfiddle 来向您展示如何操作:http://jsfiddle.net/SrwTP/
JavaScript:
var buttonTimer = setTimeout("alert('Oh noes! you didnt click the button :(');", 5000);
document.getElementById("timerButton").onclick = function(){ clearTimeout(buttonTimer); };
HTML:
<button id="timerButton">Click me or an alert will popup in 5 seconds!</button>
关于javascript - 如果用户在 x 秒内没有按下按钮则执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6291245/