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. 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
. This function returns an id that you can use to cancel the timer. 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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.