8

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?

4

5 回答 5

18

将您的href属性设置为href="javascript:delay('URL')"和 JavaScript:

function delay (URL) {
    setTimeout( function() { window.location = URL }, 500 );
}
于 2013-01-21T08:15:38.920 回答
7

如果您想延迟页面上的每个链接,您可以使用 jQuery 来完成,如下所示

$(function(){
    $("a").click(function(evt){
        var link = $(this).attr("href");
        setTimeout(function() {
            window.location.href = link;
        }, 500);
    });
});
于 2013-01-21T08:22:31.253 回答
0

要延迟内联 javascript 的链接,只需将您的href属性设置为href="javascript:setTimeout(()=>{window.location = 'URL' },500);".

当您将 URL 替换为您的链接时,只需确保它位于' '.

<li class="nav-item">
  <a href="javascript:setTimeout(()=>{window.location = '#About' },500);">
    About Me
  </a>
</li>
于 2019-07-15T19:22:48.187 回答
0

如果您想在新标签页上打开 (target="_blank"),

这将是@gurvinder372的代码更改为考虑新选项卡:

如果您想使用任何其他目标设置,只需将 更改"_blank"为您的首选设置。
function delay(URL) {
    setTimeout(function () {
        window.open(URL, "_blank"); // this is what I've changed.
    }, 500);
}
于 2021-08-07T02:21:19.683 回答
0

在继续之前,我使用它来保持功能等待:

var wait_until = new Date().getTime() + 500;
while (new Date().getTime() < wait_until) {
    //Do nothing, wait
}
于 2016-09-02T19:53:54.120 回答