[英]Javascript function not clicked whitin certain time
I need help (again). 我需要帮助(再次)。 What I would like to have is a function which if there has not been clicked one time on a button in a certain time, a div will be showed.
我想拥有的功能是,如果一定时间内未单击按钮一次,将显示一个div。 To be more clear, I'm looking for this kind of code:
更清楚地说,我正在寻找这种代码:
function MyFunction() {
if(not clicked one time in 8000 milliseconds)
document.getElementById("myDiv").style.visibility = "visible";
}
Does somebody knows how I should code this? 有人知道我该怎么编码吗? Thank you!
谢谢!
You can "set a timer" to execute a function using setTimeout
. 您可以使用
setTimeout
“设置计时器”来执行功能。 This function returns an id that you can use to cancel the timer. 此函数返回一个可用于取消计时器的ID。 Here's an example:
这是一个例子:
var clickTimeout = null; var cancel = function() { clearTimeout(clickTimeout); }; var start = function() { cancel(); document.getElementById("showMe").style.display = "none"; clickTimeout = setTimeout(onDone, 2000); }; var onDone = function() { document.getElementById("showMe").style.display = "block"; }; document.getElementById("cancel").addEventListener("click", cancel); document.getElementById("restart").addEventListener("click", start); start();
<button id="cancel">click me</button> <button id="restart">restart</button> <div id="showMe" style="display: none">Timed out</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.