It is necessary for the method to work for a certain time, for example, to output a word in the textarea for 5 seconds, which can be used for this?

  • 2
    Asynchronous, setTimeout / setInterval - andreymal
  • @andreymal, is there anything else besides this? - Anton Lyalin
  • Hardly . . . . - andreymal
  • one
    "is there anything else besides this?" - why do something else? While setTimeout() perfect. - yar85

1 answer 1

 let output = document.getElementById('output'), interval = setInterval(() => { output.textContent += 'слово '; }, 1000); setTimeout(() => { clearInterval(interval); }, 5000); 
 <textarea id="output" style="width: 300px;"></textarea> 



Or even easier:

 let interval, seconds = 0, output = document.getElementById('output'); interval = setInterval(() => { if (++seconds < 6) output.textContent += 'слово '; else clearInterval(interval); }, 1000); 
 <textarea id="output" style="width: 300px;"></textarea>