The examples in this article share two countdown effects for your reference. The specific content is as follows
Reproduction image:
1. Countdown effect
<!DOCTYPE html><html> <head> <meta charset="utf-8"/> <title>Countdown</title> <link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8"/> <style type="text/css"> *{ margin:0; padding:0;} .dtime{ margin:10px;} .dtime span{ color:#C30;} </style> </head> <body> <div id="a1"></div> <div id="a2"></div> <div id="a3"></div> <div id="a4"></div> <div id="a5"></div> </body> <script> function tbdTimeCountdown(args){//Countdown function var ele=document.getElementById(args.id); var alltime=args.alltime; var arr=[]; var t=Math.floor(alltime/(24*60*60)); var yt=alltime%(24*60*60); var s=Math.floor(yt/(60*60)); var ys=yt%(60*60); var f=Math.floor(ys/(60)); var yf=ys%(60); var m=yf; arr[0]=[t,"day"]; arr[1]=[s,"hour"]; arr[2]=[f,"minute"]; arr[3]=[m,"second end"]; var s=ce(arr,0); ele.innerHTML=s; var dong; dong=setInterval(function(){ alltime-=1; if(alltime<=0){ clearInterval(dong); ele.innerHTML="<span>0</span>second"; args.end(); }else{ t=Math.floor(alltime/(24*60*60)); yt=alltime%(24*60*60); s=Math.floor(yt/(60*60)); ys=yt%(60*60); f=Math.floor(ys/(60)); yf=ys%(60); m=yf; arr[0]=[t,"day"]; arr[1]=[s,"hour"]; arr[2]=[f,"minute"]; arr[3]=[m,"second end"]; var s=ce(arr,0); ele.innerHTML=s; }; },1000); function ce(arr,sta){ var i=sta; if(i<arr.length){ if(arr[i][0]!=0){ var s=""; for(var j=i;j<arr.length;j++){ s+="<span>"+arr[j][0]+"</span>"+arr[j][1]; }; return s; }else{ return ce(arr,i+1); }; }; }; }; }; }; }; }; //Countdown end //Effect 1 //86402 3602 62 tbdTimeCountdown({id:"a1",//Contains countdown container alltime:3,//Total countdown seconds end:function(){//Countdown end, time of 0 is the function called //alert("The countdown ends!"); }, }); //Effect 2 tbdTimeCountdown({id:"a2",//Contains countdown container alltime:62,//Total countdown seconds end:function(){//Countdown end, time 0 is the function called}, }); //Effect 3 tbdTimeCountdown({id:"a3",//Contains countdown container alltime:3602,//Total countdown seconds end:function(){//Countdown end, time 0 is the function called}, }); //Effect 4 tbdTimeCountdown({id:"a4",//Contains countdown container alltime:86402,/Total countdown seconds end:function(){//Countdown end, time 0 is the function called}, }); //Effect 4 tbdTimeCountdown({id:"a4",//Contains countdown container alltime:86402,/Total countdown seconds end:function(){//Countdown end, time 0 is the function called}, }); //Effect 4 tbdTimeCountdown({id:"a4",//Contains countdown container alltime:86402,/Total countdown seconds end:function(){//Countdown end, time 0 is the function called}, }); //Effect 4 tbdTimeCountdown({id:"a5",//Contains countdown container alltime:154789,//Total countdown seconds end:function(){//The countdown end, time 0 is the function called}, }); </script></html>2. Countdown 2.html
<!DOCTYPE html><html> <head> <meta charset="utf-8"/> <title>Countdown</title> <link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8"/> <style type="text/css"> *{ margin:0; padding:0;} .dtime{ margin:10px;} .dtime span{ color:#C30;} </style> </head> <body> <div id="a1" alltime="3"></div> <div id="a2" alltime="62"></div> <div id="a3" alltime="3605"></div> <div id="a4" alltime="48605"></div> <div id="a5" alltime="123456"></div> </body> <script> function tbdTimeCountdown(args){//Countdown function var ele=document.getElementById(args.id); var alltime=Number(ele.getAttribute(args.alltime)); var arr=[]; var t=Math.floor(alltime/(24*60*60)); var yt=alltime%(24*60*60); var s=Math.floor(yt/(60*60)); var ys=yt%(60*60); var f=Math.floor(ys/(60)); var yf=ys%(60); var m=yf; arr[0]=[t,"day"]; arr[1]=[s,"hour"]; arr[2]=[f,"minute"]; arr[3]=[m,"second end"]; var s=ce(arr,0); ele.innerHTML=s; var dong; dong=setInterval(function(){ alltime-=1; ele.setAttribute(args.alltime,alltime); if(alltime<=0){ clearInterval(dong); ele.innerHTML="<span>0</span>seconds"; args.end(); }else{ t=Math.floor(alltime/(24*60*60)); yt=alltime%(24*60*60); s=Math.floor(yt/(60*60)); ys=yt%(60*60); f=Math.floor(ys/(60)); yf=ys%(60); m=yf; arr[0]=[t,"day"]; arr[1]=[s,"hour"]; arr[2]=[f,"minute"]; arr[3]=[m,"second end"]; var s=ce(arr,0); ele.innerHTML=s; }; },1000); function ce(arr,sta){ var i=sta; if(i<arr.length){ if(arr[i][0]!=0){ var s=""; for(var j=i;j<arr.length;j++){ s+="<span>"+arr[j][0]+"</span>"+arr[j][1]; }; return s; }else{ return ce(arr,i+1); }; }; }; }; }; }; }; //Countdown end // Effect 1 //86402 3602 62 tbdTimeCountdown({id:"a1",//Contains the countdown container alltime:"alltime",//Total countdown seconds end:function(){//Countdown end, time 0 is the called function alert("The countdown end will be executed!"); }, }); //Effect 2 tbdTimeCountdown({id:"a2",//Contains the countdown container alltime:"alltime",//Total countdown seconds end:function(){//Countdown end, time 0 is the called function}, }); //Effect 3 tbdTimeCountdown({id:"a3",//Contains countdown container alltime:"alltime",//Total countdown seconds end:function(){//Countdown end, time 0 is the function called}, }); //Effect 4 tbdTimeCountdown({id:"a4",//Contains countdown container alltime:"alltime",//Total countdown seconds end:function(){//Countdown end, time 0 is the function called}, }); //Effect 4 tbdTimeCountdown({id:"a5",//Contains countdown container alltime:"alltime",//Total countdown seconds end:function(){//Countdown end, time 0 is the function called}, }); </script></html>You can refer to the special topic "JS Countdown Function Summary" for in-depth study.
The above is all about this article, and I hope it will be helpful for everyone to learn JavaScript programming.