Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript delay between adding text to a div

Tags:

javascript

I want to use Javascript to add a text to a html element (such as the p tag) 15 times and it must wait one second every time it adds the text.

I tried some ways and didn't get the proper result. It adds all 15 texts after 15 seconds.

What should I do? Please help me.

like image 247
Mohammad Javad Naderi Avatar asked May 03 '26 06:05

Mohammad Javad Naderi


1 Answers

var i = 0;
var element = document.getElementById("mydiv");
var interval = setInterval(function(){
         if(i <= 14){
              element.innerHTML += "sometext";
              }else{
            clearInterval(interval);
           }
        i++;
 },1000);
like image 50
itsme Avatar answered May 04 '26 18:05

itsme



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!