I want to delay a link for a period of 500 with ja

2019-02-10 10:40发布

I've been looking through the Stackoverflow questions, trying to get help with a simple link delay; I want to put it around a div, and I can't make heads or tails of the examples I've found.

So far, I understand that I need to halt the native function of href, but I don't know how to do that. The code is still very alien to me. Help?

3条回答
混吃等死
2楼-- · 2019-02-10 11:05

If you want to delay every link on your page, you can do it with jQuery like this

$(function(){
    $("a").click(function(evt){
        var link = $(this).attr("href");
        setTimeout(function() {
            window.location.href = link;
        }, 500);
    });
});
查看更多
Ridiculous、
3楼-- · 2019-02-10 11:06

I use this to keep the function waiting before continuing:

var wait_until = new Date().getTime() + 500;
while (new Date().getTime() < wait_until) {
    //Do nothing, wait
}
查看更多
淡お忘
4楼-- · 2019-02-10 11:10

Set your href attribute as href="javascript:delay('URL')" and JavaScript:

function delay (URL) {
    setTimeout( function() { window.location = URL }, 500 );
}
查看更多
登录 后发表回答