Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click - delay before navigating

Tags:

jquery

is there any easy way to create a code: if URL changes or clicked on a link show div (like loading gif 3-sec) then show the page? Kinda like blank white page with loading gif spin 3 sec then show the page?

Thanks!

like image 574
test Avatar asked Jul 16 '12 18:07

test


1 Answers

Given a <a class="waitBeforeNavigate" href="somewhere.html">Go somewhere</a>

function waitBeforeNavigate(ev) {
  ev.preventDefault();                    // prevent default anchor behavior
  const goTo = this.getAttribute("href"); // store anchor href

  // do something while timeOut ticks ... 

  setTimeout(function(){
    window.location = goTo;
  }, 3000);                               // time in ms
}); 

document.querySelectorAll(".waitBeforeNavigate")
  .forEach(EL => EL.addEventListener("click", waitBeforeNavigate));

Using jQuery:

$('.waitBeforeNavigate').on("click", function (ev) {
  ev.preventDefault();                // prevent default anchor behavior
  const goTo = $(this).attr("href");  // store anchor href
       
  // do something while timeOut ticks ... 
       
  setTimeout(function(){
    window.location = goTo;
  }, 3000);                           // time in ms
}); 
like image 61
Roko C. Buljan Avatar answered Oct 04 '22 22:10

Roko C. Buljan