Using the setInterval

Asked

Viewed 67 times

-2

How can I update a div using setInterval?

  • Yes, but when I use javascript my browser keeps crashing. There’s no way to do this server-side update?

  • Maybe my script was wrong. in my body, I have a div called body and I want to update it every 10 seconds. You know what javascript I should do for this update?

  • Francis, I think you’ve got it wrong. Put in the code you did and it’s a problem. The question as it stands is too broad and will not help you at all. For this reason, I will go to vote to close it. Enjoy and read the guide of [Ask].

  • 10 in 10 seconds: setInterval(function(){ document.getElementById("id_da_div").innerHTML = 'nova_informacao'; },10000);. The time is calculated in milliseconds, so 10000 = 10 seconds.

1 answer

1

You can update the div with the innerHTML within the setInterval.

Follow an example:

setInterval(function() {
  document.getElementById("relogio").innerHTML = new Date();
}, 1000);
<div id="relogio"></div>

Basic syntax of setInterval:

setInterval(<function>, <milliseconds>);

More about setInterval: https://www.w3schools.com/jsref/met_win_setinterval.asp

Browser other questions tagged

You are not signed in. Login or sign up in order to post.