Use setTimeout
to add a delay for executing code.
setTimeout(function () {
alert1 = true;
}, YOUR_DELAY_TIME_IN_MILLISECONDS); // Replace with how much time you want to wait
Your code will not work because you check it if alert1
is true at the beginning of the script running, but a few moments later alert1
will change to true
. You do not check if alert1
is true after you actually change it to true. If you want it to alert something in a few seconds, you do not need a boolean.
setTimeout(function () {
alert("Hello!")
}, 2000); // Replace with how much time you want to wait in milliseconds (this is 2000 milliseconds, which is 2 seconds)