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";
    }
}
1

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.