2

我有一个像这样的跨度元素:

  <span id="test" name="testing" >Test </span>

span ie Test 的文本可能会根据页面中的其他操作而改变。我想在 2 秒后自动刷新 span 文本。如何使用 jquery 实现这一点?请帮忙。

4

4 回答 4

3

您可以使用setTimeoutsetInterval。如果您想重复一次,请使用前者,如果您想在 2 秒后连续刷新,请使用后者:

setTimeout(function() { $("#test").text("text from other operations"); },2000);

或者

setInterval(function() { $("#test").text("text from other operations"); },2000);
于 2012-07-31T17:20:36.043 回答
2

您可以使用 javascript setInterval 创建类似计时器的效果。https://developer.mozilla.org/en/DOM/window.setInterval

代码将类似于:

setInterval(function(){
    $('#test').text('newtext');
}, 2000);
于 2012-07-31T17:21:05.763 回答
0
var interval = setInterval(dostuff, 2000);

function dostuff() {
    /* do stuff */
    $('span#test').html('some stuff');
}
于 2012-07-31T17:21:15.300 回答
0
function updateSpan(){
  var newText;
  // Compute new text here
  document.getElementById("test").innerHTML=newTest;
}
setInterval("updateSpan()",2000);

这甚至可以在没有 jQuery 的情况下使用。

于 2012-07-31T17:21:42.623 回答