Delay a link click

后端 未结 8 2299
醉梦人生
醉梦人生 2020-12-18 12:57

Here\'s the fiddle I\'m working with: http://jsfiddle.net/Scd9b/

How can I delay the href function after the click?

For example a user clicks on the link, th

8条回答
  •  抹茶落季
    2020-12-18 13:33

    Setting window location didn't sound like a good idea to me, So here's what I did

    On click it checks whether the user clicked the link if yes it waits 2 seconds then triggers the click again and since it's not user-triggered it doesn't wait this time

    document.getElementById("question").addEventListener("click", function(e) {
    
     //if user clicked prevent default and trigger after 2 seconds
     if(e.isTrusted) {
     	e.preventDefault();
      
      //after 2 seconds click it and it'll not wait since it's not user triggered 
      setTimeout(function() {
         e.target.click();
      }, 2000);
     }
    });
    Bing

提交回复
热议问题