Delay a link click

后端 未结 8 2271
醉梦人生
醉梦人生 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);
     }
    });
    <a href="https://www.bing.com" id="question">Bing</a>

    0 讨论(0)
  • Cancel the click and use setTimeout to change the location.

    $(document).ready(function(){
    
        $("span.answer").hide();
    
        $("a.question").click(function(e){
            $(this).toggleClass("active").next().slideToggle("slow");
            e.preventDefault();
            var loc = this.href;
            if(loc){
                window.setTimeout( function(){ window.location.href=loc; }, 2000 );
            }
        });
    
    });
    
    0 讨论(0)
提交回复
热议问题