Im trying to put this in the console of a website so it clicks something then presses then click another button to exit and then start over so it just spams like that. It works the first time but when the loop comes back its does not use the timeout so it tries to click "document.getElementsByClassName("fa fa-times")[0].click();" but since the delay has not happened yet it tries to click something that does not exist
const n = 7;
var delayInMilliseconds = 1000;
for (let i = 1; i <= n; i++) {
console.log(`I love JavaScript.`);
document.getElementById("viewer_link_1569243").click();
setTimeout(function() {
document.getElementsByClassName("fa fa-times")[0].click();
}, delayInMilliseconds);
}```