我的页面上有一个按钮。我希望该按钮每 2/4 秒使用 Javascript 更改一次语言。例如当页面加载时,将搜索按钮的文本,并在 2 或 4 秒后更改为其他语言。不需要死循环,最简单即可。
HTML:
<button id="search" name="q">search</button>`
Javascript:
var x = document.getElementById('search');
//after 2 seconds:
x.innerHTML="Suchen";
//And so on
最佳答案
这是针对您的问题的最可靠、最简单的解决方案。 JSFIDDLE .
使用 setInterval()
var x = document.getElementById('search'),
// dictionary of all the languages
lan = ['Search', 'Suchen', 'other'],
// hold the spot in the dictionary
i = 1;
setInterval(function (){
// change the text using the dictionary
// i++ go to the next language
x.innerHTML = lan[i++];
// start over if i === dictionary length
i = lan.length === i ? 0 : i;
}, 2000);
关于javascript - 如何在 Javascript 中随时间更改按钮文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19547592/