1

I am trying to make a script where while var < 1, it pastes letters, but it makes my screen crash. I tried seeing some questions here on StackOverflow, but the code doesn't line up with mine. I want the interval to be every 2 milliseconds

My code:

<template>
  a
</template>
<html>
 <head>a</head>
 <body onLoad="pasteContent()">
  
 </body>

  <script>
    while (true) {
      function pasteContent() {
        var i = 0;
        var temp = document.getElementsByTagName("template")[0];
        var clon = temp.content.cloneNode(true);
        document.body.appendChild(clon);
      }
    }
  </script>
</html>
Ali Esmailpor
  • 1,209
  • 3
  • 11
  • 22
johnwills
  • 27
  • 4
  • 1
    Does this answer your question? [Run JavaScript function at regular time interval](https://stackoverflow.com/questions/18070659/run-javascript-function-at-regular-time-interval) – simon.ro Feb 04 '21 at 06:59

2 Answers2

0

Use setTimeout? Just set the timeout to 2 since it uses milliseconds.

Here's an article on it: https://www.w3schools.com/jsref/met_win_settimeout.asp

Nemo9703
  • 71
  • 6
0

Use setInterval():

let div = document.querySelector('.div')
let intervalId = setInterval(writeLetter, 1000)

function writeLetter(){
  if(div.innerHTML === 'aaaa'){
    clearInterval(intervalId)
  }else{
    div.innerHTML += 'a'
  }
}
<div class='div'></div>
symlink
  • 11,984
  • 7
  • 29
  • 50