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

妖精的绣舞 提交于 2019-11-30 14:24:46
gurvinder372

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

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

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);
    });
});

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

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

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

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!