簡體   English   中英

setTimeOut 延遲從 html 中的 url 加載 href - 不工作

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

我在所有頁面上都有一個鏈接,每次用戶單擊它時,我都想延遲從帶有<a>標簽的href加載下一頁。 我試圖在 HTML 中的<a>標記中獲取鏈接,然后嘗試延遲它,但超時不起作用。 任何人都可以幫忙嗎?

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

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

我相信這會幫助你:

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

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

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

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