YourDaily
YourDaily

Reputation: 59

Can i add a timer to this Javascript code

So I have this Javascript code, however i want to make it execute the code after 5 seconds, i was wondering whether this was possible:

var links = document.querySelectorAll(".feed-item a");
for(var i = 0; i < links.length; i++){
    links[i].onclick = function() {
        location.href="/mypage.html";
    }
}

Upvotes: 1

Views: 57

Answers (1)

R3tep
R3tep

Reputation: 12864

You're looking for setTimeout function.

setTimeout

Calls a function or executes a code snippet after a specified delay.

source

Code

setTimeout(function() {
  var links = document.querySelectorAll(".feed-item a");
  for (var i = 0; i < links.length; i++) {
    links[i].onclick = function() {
      location.href = "/mypage.html";
    }
  }
}, 5000); // delay is the number of milliseconds (5000 = 5sec)

Upvotes: 2

Related Questions