我需要为我的网站创建一个计时器。我正在设置一个包含多项选择题的表单。我希望用户在30分钟的指定时间提交表单/答案,如果时间过去,则提交按钮被禁用并发出警告消息;时间到了,测试失败了。 帮助整个代码; Jquery和PHP Pease;
答案 0 :(得分:0)
为此,您可以使用setTimeOut()
javascript函数。
以下行例如在3秒后显示带有文本Hello的警告框。
setTimeout(function(){ alert("Hello"); }, 3000);
在您的情况下,您要禁用提交按钮并在 30分钟之后显示提醒,30分钟= 30 * 60 = 1800秒,等于1800000毫秒。
所以代码变成这样:
setTimeout(function(){
//disable the button with id="submitbutton"
document.getElementById('submitbutton').disabled = true;
alert("Time out!");
}, 1800000);
要在页面加载后使其工作,您需要将其放在javascript文件或<script></script>
标记中并将其包装在on document ready事件中。完整的代码是:
<html>
<head>
</head>
<body>
Your HTML here with the <input type="submit" id="submitbutton">
<script>
// self executing function
(function() {
setTimeout(function(){
//disable the button with id="submitbutton"
document.getElementById('submitbutton').disabled = true;
alert("Time out!");
}, 1800000);
})();
</script>
</body>
</html>