I’m trying to make a script that clicks on a page button, waits X seconds (for the result of the click to take place), and then continues.
How can I implement the waiting part?
I’m trying to make a script that clicks on a page button, waits X seconds (for the result of the click to take place), and then continues.
How can I implement the waiting part?
You want to use setTimeout()
which executes a code snippet after a specified delay.:
var timeoutID;
function delayedAlert() {
timeoutID = setTimeout(slowAlert, 2000);
}
function slowAlert() {
alert("That was really slow!");
}
function clearAlert() {
clearTimeout(timeoutID);
}
<p>Live Example</p>
<button onclick="delayedAlert();">Show an alert box after two seconds</button>
<p></p>
<button onclick="clearAlert();">Cancel alert before it happens</button>
Or you can use setInterval()
which calls a function or executes a code snippet repeatedly, with a fixed time delay between each call to that function:
function KeepSayingHello(){
setInterval(function () {alert("Hello")}, 3000);
}
<button onclick="KeepSayingHello()">Click me to keep saying hello every 3 seconds</button>
using setTimeout, which executes only once after the delay provided
using setInterval , which executes repeatedly after the delay provided
clearTimeout
andclearInterval
is used to clear them up !!!