简体   繁体   English

setTimeOut 延迟从 html 中的 url 加载 href - 不工作

[英]setTimeOut to delay load of href from url in html - Not working

I have a link on all pages and every time the user clicks it I want to delay the next page load from the href with the <a> tag.我在所有页面上都有一个链接,每次用户单击它时,我都想延迟从带有<a>标签的href加载下一页。 I have tried to grab the link in the <a> tag in the HTML then tried to delay it but the timeout won't work.我试图在 HTML 中的<a>标记中获取链接,然后尝试延迟它,但超时不起作用。 Can anyone kindly help?任何人都可以帮忙吗?

var link = document.querySelector('a').getAttribute('href')

setTimeout(function() {
  location.href = link
}, 4000);

This will help you I believe:我相信这会帮助你:

const a = document.querySelector('a');

a.addEventListener("click", (e) => {
  e.preventDefault();
  const link = e.target.href;

  setTimeout(() => {
    window.open(link)
  }, 1000)
})

Working example on jsfiddle jsfiddle 上的工作示例

 if ( document.getElementById('primaryCTA') != null) {
var primaryCTA = document.getElementById("primaryCTA").onclick = showSpinner;

function showSpinner() { 
var spin = document.getElementById("tick-animation-small");
if(spin.style.display == 'inline-flex')
spin.style.display = 'none';
else
spin.style.display = 'inline-flex';

var  saving = document.getElementById("mini-spinner-copy"); 
if(saving.innerHTML ==  "Continue" )
    saving.innerHTML = "Saving progress"; 
else
    saving.innerHTML ==  "Continue";
  
    var element = document.getElementById("mini-spinner-copy");
    element.classList.add("fadeIn");     
  
 

 const a = document.querySelector('a');

 a.addEventListener("click", (e) => {
   e.preventDefault();
   const link = e.target.href;

   setTimeout(() => {
      window.open(link)
   }, 3000)
 })
 
  
};

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM