Preface: In many pages, we can see some countdown or time-related effects. Today, Xiao Xiong will give an overview of the countdown in JavaScript. 1. setTimeout() timergrammar: window.setTimeout(call function, [delay in milliseconds]);
The code is as follows: window.setTimeout(function(){ alert('Hello'); },5000); The running results are: It should be noted that:
2. Stop the setTimeout() timerOnce we have created a timer, what should we do if we want to cancel it? The function to clear the timer is used as follows: window.clearTimeout(timeoutID)
For example: In the above case, if we want to stop it before the specified event, we can first add a click button and add an event to clear the timer to this button. The operation is: var hello = window.setTimeout(function(){ alert('Hello'); },5000); var btn = document.querySelector('button'); btn.addEventListener('click',function(){ window.clearTimeout(hello); }) The running effect is: It can be seen that when we do not click the stop button, 'Hello' pops up after five seconds. After refreshing the page, when we click the button, no matter how long it takes, there will be no pop-up window, and the timer is cleared successfully. 3. setInterval() timerLet's look at another type of timer. window.setInterval(callback function, [interval in milliseconds]);
For example: Let's write a timer to print 'hello' every second. The code is: setInterval(function(){ console.log('Hello') },1000); The running effect is: 4. Clear the setInterval() timerSimilarly, we can also clear the effect of the setInterval() timer. The syntax is: window.clearInterval(intervalID);
For example, we now have two buttons. Clicking one can start the timer, and clicking the other can clear the timer. The operation method is: <body> <button class='begin'>Start</button> <button class='stop'>Stop</button> <script> var btn = document.querySelectorAll('button'); var timer = null; btn[0].addEventListener('click',function(){ timer = setInterval(function(){ console.log('Hello'); },1000) }) btn[1].addEventListener('click',function(){ clearInterval(timer) }) </script> </body> The running effect is: 5. Electronic clock caseWe can now make an electronic clock that displays the current year, month, day, hour, minute, and second, and allows them to change automatically. The code is as follows: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> div { width: 500px; margin: 100px auto; font-size: 25px; } </style> </head> <body> <div></div> <script> var div = document.querySelector('div'); function showTime(){ var date = new Date(); var y = date.getFullYear(); var m = date.getMonth()+1; m = m>=10?m:'0'+m; var d = date.getDate(); d = d>=10?d:'0'+d; var h = date.getHours(); h = h>=10?h:'0'+h; var dm = date.getMinutes(); dm = dm>=10?dm:'0'+dm; var ds = date.getSeconds(); ds = ds>=10?ds:'0'+ds; var str = y+'year'+m+'month'+d+'day'+h+'hour'+dm+'minute'+ds+'second'; div.innerHTML = str; setTimeout(showTime,1000); } window.onload = showTime(); </script> </body> </html> The running effect is: This is the end of this article about the principles of JavaScript timers. For more relevant JavaScript timer content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Sample code for programmatically processing CSS styles
>>: HTML discount price calculation implementation principle and script code
This article uses vue, and adds mouse click event...
Table of contents 1. Steps 1. Define buttom permi...
Table of contents Overview Subqueries Subquery Cl...
HTML consists of two parts: head and body ** The ...
A few days ago, I discovered that my website was ...
This article shares a collection of Java problems...
Table of contents 1. Solution 1 (UDF) Demo Case 2...
This blog post is about a difficulty encountered ...
Table of contents nextTick v-model syntax sugar ....
Problem Description html <iframe id="h5Co...
The role of the interface: Interface, in English:...
1. Arrange CSS in alphabetical order Not in alphab...
Table of contents 1. Basic knowledge of indexing ...
Table of contents 1. What is dynamic typing? 2. D...
Why do we need to summarize the browser compatibi...