In order to enhance the ability to write JavaScript native code and consolidate the use of setTimeout(), a countdown demo was made. Countdown is a common small function in today's websites. If you like it, you can keep it and treat it as a practical small script for daily use. Implementation ideas 1. Get the hour value first, subtract 1 from the hour value and start the countdown. Minutes 59 Seconds 59 Implementation Code html <p>Countdown:</p> <span id="hour">5</span> <span>:</span> <span id="minuteTen">0</span> <span id="minuteBit">0</span> <span>:</span> <span id="secondTen">0</span> <span id="secondBit">0</span> CSS span{ display: inline-block; width: 20px; height: 20px; background-color: #000000; color: #ffffff; text-align: center; } JavaScript function time(){ /*Hour*/ var hourTxt = document.getElementById("hour"); var hour = parseInt(document.getElementById("hour").innerHTML); /*minute*/ var minuteTenTxt = document.getElementById("minuteTen"); var minuteBitTxt = document.getElementById("minuteBit"); var minuteTen = parseInt(document.getElementById("minuteTen").innerHTML); var minuteBit = parseInt(document.getElementById("minuteBit").innerHTML); /*Second*/ var secondTenTxt = document.getElementById("secondTen"); var secondBitTxt = document.getElementById("secondBit"); var secondTen = parseInt(document.getElementById("secondTen").innerHTML); var secondBit = parseInt(document.getElementById("secondBit").innerHTML); function start(){ hour--; hourTxt.innerHTML = hour; minuteTen = 5; minuteTenTxt.innerHTML = minuteTen; minuteBit = 9; minuteBitTxt.innerHTML = minuteBit; secondTen = 5; secondTenTxt.innerHTML = secondTen; secondBit = 9; secondBitTxt.innerHTML = secondBit; /*secondBit starts to decrease*/ function second(){ secondBit--; secondBitTxt.innerHTML = secondBit; /*Ten seconds later*/ if(secondBit < 0){ secondTen--; secondTenTxt.innerHTML = secondTen; secondBit = 9; secondBitTxt.innerHTML = secondBit; /*Continue countdown*/ setTimeout(second,1000); /*One minute later*/ if(secondTen < 0){ minuteBit--; minuteBitTxt.innerHTML = minuteBit; secondTen = 5; secondTenTxt.innerHTML = secondTen; secondBit = 9; secondBitTxt.innerHTML = secondBit; /*Ten minutes later*/ if(minuteBit < 0){ minuteTen--; minuteTenTxt.innerHTML = minuteTen; minuteBit = 9; minuteBitTxt.innerHTML = minuteBit; } /*After one hour*/ if(minuteTen < 0){ hour--; hourTxt.innerHTML = hour; minuteTen = 5; minuteTenTxt.innerHTML = minuteTen; minuteBit = 9; minuteBitTxt.innerHTML = minuteBit; secondTen = 5; secondTenTxt.innerHTML = secondTen; secondBit = 9; secondBitTxt.innerHTML = secondBit; } /*Countdown ends*/ if(hour < 0 ){ hour = 0; hourTxt.innerHTML = hour; minuteTen = 0; minuteTenTxt.innerHTML = minuteTen; minuteBit = 0; minuteBitTxt.innerHTML = minuteBit; secondTen = 0; secondTenTxt.innerHTML = secondTen; secondBit = 0; secondBitTxt.innerHTML = secondBit; clearTimeout(second); clearTimeout(start); } } }else{ setTimeout(second,1000); } } setTimeout(second,1000); } setTimeout(start,1000); } Execution Page End Page The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
>>: Installation, activation and configuration of ModSecurity under Apache
This article shares the MySQL precompilation func...
This article introduces how to create an index on...
1. Download and decompress MySQL 8.0.20 Download ...
This article attempts to write a demo to simulate...
This article example shares the specific implemen...
Here is a text scrolling effect implemented with ...
This article uses examples to describe advanced u...
1. Download mysql-5.7.21-windowx64.zip from the o...
1. Download the gitlab image docker pull gitlab/g...
Deleting a table is not very common, especially f...
Scary, isn't it! Translation in the picture: ...
First download the dependencies: cnpm i -S vue-uu...
Copy code The code is as follows: <html xmlns=...
In order to download this database, it takes a lo...
A very useful function group_concat(), the manual...