Automatically refresh span text

11,602

Solution 1

You can use the javascript setInterval to create a timer like effect. https://developer.mozilla.org/en/DOM/window.setInterval

Code will be something like:

setInterval(function(){
    $('#test').text('newtext');
}, 2000);

Solution 2

You can use either setTimeout or setInterval. if you want to repeat it once, use the former, if you want to refresh continually after a span of 2 seconds, use the latter:

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

or

setInterval(function() { $("#test").text("text from other operations"); },2000);
Share:
11,602
kuchiku
Author by

kuchiku

Updated on September 12, 2022

Comments

  • kuchiku
    kuchiku almost 2 years

    I have a span element like this:

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

    The text of span i.e Test might change based on other operations in the page. I want to automatically refresh the span text say after 2 seconds. How can I achieve this using jquery? Please help.

    • Diodeus - James MacFarlane
      Diodeus - James MacFarlane almost 12 years
      Refresh it from what data source?
    • Biotox
      Biotox almost 12 years
      Do you want continuous refreshing or 2 seconds after the pages loads? Can you add your HTML and any jQuery you've tried?
    • kuchiku
      kuchiku almost 12 years
      I am using the following for testing: setInterval(function(){ $('#test').html('hello'); },2000);
    • kuchiku
      kuchiku almost 12 years
      How to reload it with same value ?
    • kuchiku
      kuchiku almost 12 years
      Ok, first I would like to know how to just refresh its original value every 2 secs.