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);
Author by
kuchiku
Updated on September 12, 2022Comments
-
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 almost 12 yearsRefresh it from what data source?
-
Biotox almost 12 yearsDo you want continuous refreshing or 2 seconds after the pages loads? Can you add your HTML and any jQuery you've tried?
-
kuchiku almost 12 yearsI am using the following for testing: setInterval(function(){ $('#test').html('hello'); },2000);
-
kuchiku almost 12 yearsHow to reload it with same value ?
-
kuchiku almost 12 yearsOk, first I would like to know how to just refresh its original value every 2 secs.
-