The jquery implementation determines whether the countdown code ends

  • 2020-12-16 05:51:40
  • OfStack

1 code examples, this chapter introduces the code to determine whether the current date is end of the countdown, countdown to this code and there is no effect, only determine whether the countdown to complete, such as shopping website preferential period etc, even though the actual application, rarely appear similar code, but hope to be able to bring visitors 1 set.

The code is as follows:


function done(){
var str=$('#end').text(); 
var out=str.match(/\d+/g);
console.log(out); 
var h=parseInt(out[0]),m=parseInt(out[1]),s=parseInt(out[2]); 
console.log(h+'#'+m+'#'+s);
var calc=h*3600+m*60+s;
console.log(calc); 
if(calc==0){
//code
} 
else{
console.log(' Waiting for the ..');
} 
var t=setTimeout('done()',1000);
} 
done(); 

The above is only code snippet, can not demonstrate, the following describes its implementation process 1.

1. Code comments:

1.function done(){}, this function can judge the end effect of the countdown.
var str=$('#end').text(), gets the text content of the specified element. This code should count down the current time.
3.var out= str. match(/\d+/g), gets an array of numbers in the time and date.
4.var h=parseInt(out[0]),m=parseInt(out[1]),s=parseInt(out[2]), acquiring hours, minutes and seconds, respectively.
5.var calc=h*3600+m*60+s, converted to seconds.
6.if(calc==0){//code}, judge whether the countdown is over, and then specify the corresponding operation.
7.var t=setTimeout('done()',1000), which executes the judgment function every 1 second.
8.done(), execute this function.

The countdown code of jquery implementation is as follows:


$(function(){ 
var tYear = ""; // Year of input  
var tMonth = ""; // Month of input  
var tDate = ""; // Date entered  
var iRemain = ""; // The number of milliseconds between the beginning and the end  
var sDate = ""; // Days counted backwards  
var sHour = ""; // Counting down the hours  
var sMin = ""; // Minutes to count down  
var sSec = ""; // Seconds count down  
var sMsec = ""; // Number of milliseconds  
// Universal utility function, add zero to the units digit, according to the pass N Let's say I put some zeros in front of it  
function setDig(num,n){ 
var str = ""+num; 
while(str.length<n){ 
str="0"+str 
} 
return str; 
} 
// Get the difference in days, hours, minutes, seconds  
function getdate(){ 
// Create date objects for start and end times  
var oStartDate = new Date(); 
var oEndDate = new Date(); 
// Gets the value of the text box  
tYear = $("#tyear").val(); 
tMonth = $("#tmonth").val(); 
tDate = $("#tdate").val(); 
// Set end time  
oEndDate.setFullYear(parseInt(tYear)); 
oEndDate.setMonth(parseInt(tMonth)-1); 
oEndDate.setDate(parseInt(tDate)); 
oEndDate.setHours(0); 
oEndDate.setMinutes(0); 
oEndDate.setSeconds(0); 
// Figure out the number of seconds to start and end ( Divided by the 1000) 
iRemain = (oEndDate.getTime() - oStartDate.getTime())/1000; 
// The total number of seconds divided by 1 The number of seconds of days, and then take out the integer part, you get how many days.  
sDate = setDig(parseInt(iRemain/(60*60*24)),3); 
// The total number of seconds divided by 1 The number of seconds in a day, and then you take the remainder, which is the total number of seconds left after you subtract the whole number of days.  
iRemain %= 60*60*24; 
// The total number of seconds left divided by 1 The number of seconds in hours, in integers, is how many hours there are.  
sHour = setDig(parseInt(iRemain/(60*60)),2) 
// The total number of seconds left divided by 1 The number of seconds in hours, and then you take the rest, and the remainder, is the total number of seconds left after you subtract the hours.  
iRemain %= 60*60; 
// The total number of seconds left divided by 1 The number of seconds in a minute, taken as an integer, is how many minutes there are.  
sMin = setDig(parseInt(iRemain/60),2) 
// The total number of seconds left divided by 1 The number of seconds in the minute, and then you take the rest of the number, the remainder, is the total number of seconds left after you subtract the minutes.  
iRemain%=60; 
// Seconds left  
sSec = setDig(iRemain,2); 
// Number of milliseconds  
sMsec = sSec*100; 
} 
// Change the display time  
function updateShow(){ 
$(".showdate span").text(tYear+"-"+tMonth+"-"+tDate); 
$(".count span").each(function(index, element) { 
if(index==0){ 
$(this).text(sDate); 
}else if(index==1){ 
$(this).text(sHour); 
}else if(index == 2){ 
$(this).text(sMin); 
}else if(index == 3){ 
$(this).text(sSec); 
}else if(index == 4){ 
$(this).text(sMsec); 
} 
}); 
} 
// every 1 Seconds to perform 1 Subtemporal update  
function autoTime(){ 
getdate(); 
// If less than zero, clear the call itself, and return  
if(iRemain<0){ 
clearTimeout(setT); 
return; 
} 
updateShow(); 
var setT = setTimeout(autoTime,1000); 
} 
// Click the button to start the timer  
$("button").click(function(){ 
autoTime(); 
}) 
})

Note the following points:

1. Modulus operation:

iRemain %= 60*60*24;

It returns the remainder, the remainder in this instance, the number of seconds left after taking away the integer.

2. Utility function setDig(num,n)

You can automatically add zero to the number you pass in based on the parameters


Related articles: