JavaScript/jQuery或每隔几秒钟更改一次文本
我需要 JavaScript 或 jQuery 来每隔几秒更改一次文本...无需用户做任何事情.
I need JavaScript or jQuery something to change text every few seconds... without the user doing anything.
例子:
欢迎"更改为Salmat datang";3 秒后变为Namaste"等并循环返回.
"Welcome" changes to "Salmat datang" changes to "Namaste", etc. after 3 seconds and loops back.
推荐答案
正如其他人所说,setInterval
是你的朋友:
As others have said, setInterval
is your friend:
var text = ["Welcome", "Hi", "Sup dude"];
var counter = 0;
var elem = document.getElementById("changeText");
var inst = setInterval(change, 1000);
function change() {
elem.innerHTML = text[counter];
counter++;
if (counter >= text.length) {
counter = 0;
// clearInterval(inst); // uncomment this if you want to stop refreshing after one cycle
}
}
<div id="changeText"></div>
相关文章