簡體   English   中英

setinterval問題重復功能

[英]setinterval problems repeating function

我希望通過應用此功能從頁面頂部下拉一些單詞

function dropWord(){
  var word = document.createElement("div");
  body.insertBefore(word, document.getElementsByTagName("script")[0]);
  word.classList.add("word");
  var random = (Math.random() * (99 - 0)).toFixed(1);
  word.style.left = `${random}%`
}

並每3秒重復一次此功能

setInterval(dropWord(), 3000);

該功能一旦起作用便無法繼續執行,我確定它正在執行一些愚蠢的錯誤,我們將不勝感激

setInterval需要函數引用。 您實際上是在調用該函數。 你需要這個:

setInterval(dropWord, 3000); // notice the lack of () in the function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM