showbox
is all you need. Remove the ()
.
setTimeout doesn't work [duplicate]
-
12-07-2023 - |
Question
I have made a div with id "about" with display:none and I wrote this script :
function showbox() {
document.getElementById('about').style.display="block"
};
And added the function as an event to another div :
<div class="button" onClick="slide();setTimeout(showbox(),3000)">About</div>
So that when the user click on the div with class "button" the div with id "about" will appear after 3 seconds, the code works with the showing part but it doesn't delay anything ! Note: the function slide() exists and works perfectly and it's not interfering with anything so don't question it.
Please tell me what's wrong. Thanks.
Solution 3
OTHER TIPS
// You are *calling* showbox() here -- not passing the function ref as a parameter.
// Use this instead.
<div class="button" onClick="slide();setTimeout(showbox,3000)">About</div>
Better methodology: separation of concerns. Inline javascript is bad practice. Just us an event listener!
var element; //grab element however you want
element.addEventListener("click",function() {
slide();
setTimeout(showbox,3000);
},false);
As the others said, you need to pass a function reference to the timeout. Having the ()
immediately calls the function.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow