Home > Web Front-end > HTML Tutorial > How to stop the execution of a function using JavaScript?

How to stop the execution of a function using JavaScript?

王林
Release: 2023-09-15 12:37:02
forward
1298 people have browsed it

How to stop the execution of a function using JavaScript?

To stop the execution of a function in JavaScript, use the clearTimeout() method. This function call clears any timers set by the setTimeout() function.

Example

You can try running the following code to learn how to use the clearTimeout() method in JavaScript.

<html>
   <head>
      <title>JavaScript clearTimeout() method</title>
      <script>
         <!--
            var imgObj = null;
            var animate ;
           
            function init(){
               imgObj = document.getElementById(&#39;myImage&#39;);
               imgObj.style.position= &#39;relative&#39;;
               imgObj.style.left = &#39;0px&#39;;
            }
            function moveRight(){
               imgObj.style.left = parseInt(imgObj.style.left) + 10 + &#39;px&#39;;
               animate = setTimeout(moveRight,20);    // call moveRight in 20msec
            }
            function stop(){
               clearTimeout(animate);
               imgObj.style.left = &#39;0px&#39;;
            }
            window.onload = init;
         //-->
      </script>
   </head>
   <body>
      <form>
         <img  id = "myImage" src = "/images/html.gif" / alt="How to stop the execution of a function using JavaScript?" >
         <p>Click the buttons below to handle animation</p>
         <input type = "button" value = "Start" onclick = "moveRight();" />
         <input type = "button" value = "Stop" onclick = "stop();" />
      </form>
   </body>
</html>
Copy after login

The above is the detailed content of How to stop the execution of a function using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template