1

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";
    }
}
R3tep
  • 12,512
  • 10
  • 48
  • 75
YourDaily
  • 59
  • 9
  • Possible duplicate of [Loop timer in javascript](http://stackoverflow.com/questions/2133166/loop-timer-in-javascript) – MeanGreen Oct 14 '15 at 14:03

1 Answers1

2

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)
R3tep
  • 12,512
  • 10
  • 48
  • 75