我想使用 javascript 将链接延迟 500 年

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14434604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:39:09  来源:igfitidea点击:

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

javascripthtmltimehyperlinkdelay

提问by ammonhra

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.

我一直在查看 Stackoverflow 问题,试图获得有关简单链接延迟的帮助;我想把它放在一个 div 周围,我无法对我找到的例子做出正面或反面。

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?

到目前为止,我知道我需要停止 href 的本机功能,但我不知道该怎么做。代码对我来说仍然很陌生。帮助?

回答by gurvinder372

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

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

function delay (URL) {
    setTimeout( function() { window.location = URL }, 500 );
}

回答by ttkalec

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

如果你想延迟页面上的每个链接,你可以像这样使用 jQuery

$(function(){
    $("a").click(function(evt){
        var link = $(this).attr("href");
        setTimeout(function() {
            window.location.href = link;
        }, 500);
    });
});

回答by nebulousGirl

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
}

回答by nattik Gur-Arie

To delay a link with inline javascript, just set your hrefattribute as href="javascript:setTimeout(()=>{window.location = 'URL' },500);".

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

When you replace the URL with your link, just make sure it is inside the ' '.

当您用链接替换 ​​URL 时,只需确保它在' '.

<li class="nav-item">
  <a href="javascript:setTimeout(()=>{window.location = '#About' },500);">
    About Me
  </a>
</li>