在我的下面的代碼中,這些單詞被隨機推送到div。這個單詞應該在每5秒後改變一次。它在正常情況下工作。如果我點擊「新建」和「看到」這個詞每3秒鐘立即改變一次,但我給了5秒改變。我不明白爲什麼它在幾秒鐘內改變。請檢查我的下面的代碼。如何快速解決時間間隔runniung?
wordlist = []
wordlist.push({
word: "ABUNDANCES",
seen: false
})
wordlist.push({
word: "BOTANOMANCY",
seen: false
})
wordlist.push({
word: "CALUMNIATED",
seen: false
})
wordlist.push({
word: "DECISIONING",
seen: false
})
wordlist.push({
word: "ENCAPSULATE",
seen: false
})
wordlist.push({
word: "FINGERNAILS",
seen: false
})
wordlist.push({
word: "GALLIVANTED",
seen: false
})
wordlist.push({
word: "HEADBANGING",
seen: false
})
wordlist.push({
word: "IMPASSIONED",
seen: false
})
wordlist.push({
word: "JEWELFISHES",
seen: false
})
wordlist.push({
word: "KILOGRAMMES",
seen: false
})
wordlist.push({
word: "LAMPLIGHTER",
seen: false
})
wordlist.push({
word: "MANICURISTS",
seen: false
})
wordlist.push({
word: "NEPHROTOXIC",
seen: false
})
function loadRandom() {
n = Math.floor(Math.random() * wordlist.length);
$("#status").text(wordlist[n].word);
}
$(function() {
var interval = setInterval(loadRandom, 5000);
});
loadRandom(); // for initial display
function seen() {
loadRandom();
}
function unseen() {
loadRandom();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="status" class="keyword">Test</div>
<a class="btn waves-effect red" id="seen" onClick="seen()">Seen</a>
<a class="btn waves-effect red" id="unseen" onClick="unseen()">New</a>
請澄清你想要什麼?你設置間隔5秒,但你希望間隔快速運行。如果是這樣,你可以把間隔小於5000(5秒) – Sovary
在正常情況下,它在5秒內更改單詞。如果我點擊「新建」和「已看到」按鈕,它會在3秒內改變。但我給定的時間間隔爲5000 – user7393779