What is the role of clearTimeout() function in JavaScript?

JavascriptWeb DevelopmentFront End Technology

If you have set a time using setTimeout() function, then you can clear it using the JavaScript clearTimeout() function.


You can try to run the following code learn how to implement clearTimeout() function in JavaScript −

      <title>JavaScript Animation</title>
            var imgObj = null;
            var animate ;
            function init() {
               imgObj = document.getElementById('myImage');
               imgObj.style.position= 'relative';
               imgObj.style.left = '0px';
            function moveRight() {
               imgObj.style.left = parseInt(imgObj.style.left) + 10 + 'px';
               animate = setTimeout(moveRight,20); // call moveRight in 20msec
            function stop() {
               imgObj.style.left = '0px';
            window.onload =init;
         <img id = "myImage" src = "/images/html.gif" />
         <p>Click the buttons below to handle animation</p>
         <input type = "button" value = "Start" onclick = "moveRight();" />
         <input type="button" value="Stop" onclick="stop();" />
Published on 23-Feb-2018 15:28:13