Javascript Data Type How to - Initialize a repetitive action with getTime()








Question

We would like to know how to initialize a repetitive action with getTime().

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
#box {<!-- ww w . j a  v a2s .  c o  m-->
  height: 100px;
  width: 100px;
  background-color: red;
}
</style>
</head>
<body>
  <div id="box"></div>
  <script type='text/javascript'>
var isBlue = false;
var box = document.getElementById('box');
setInterval(function() {
    box.style.backgroundColor = isBlue ? "red" : "blue";
    isBlue = !isBlue;
}, 500);

</script>
</body>
</html>

The code above is rendered as follows: