Javascript DOM How to - Set node text in delay








Question

We would like to know how to set node text in delay.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.onload=function(){<!--from  w w  w .  j a va 2 s . c  o  m-->
    var test = document.getElementById("test");
    var a = document.createTextNode("");
    test.appendChild(a);
    setInterval(function(){
        a.nodeValue = "Test test test";
    },100);
}
</script>
</head>
<body>
  Test test test
  <div id="test"></div>
</body>
</html>

The code above is rendered as follows: