javascript - 如何在 Javascript 中随时间更改按钮文本?

标签 javascript html time

我的页面上有一个按钮。我希望该按钮每 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/

相关文章:

html - CSS:元素本身的过渡属性?

javascript - JS querySelector 只有第一个 child 没有迭代 child

php - 这是什么类型的时间戳,我需要将其转换为人类可读的时间戳

Java Date 给出的时差不正确,提前 1 小时

javascript - Jquery 单独的多选选项更新价格变化

javascript - 使用 jqGrid 进行内联编辑时在表格上方显示表单控件

java - 如何使用java在电子邮件正文中发送动态html表

php - 使用 php 和 mysql 存储和选择时间/日期的最佳实践?

javascript - 在 JavaScript RPG 中使用 "more random"RNG 有多重要?

javascript - 这个xhr请求在axios中怎么写?