SoFunction
Updated on 2025-04-10

JavaScript implements the countdown program for snap-up purchases

This article has shared the specific code for javascript to implement rush purchase countdown for your reference. The specific content is as follows

The countdown is divided into three states:

1) The start time of the rush purchase is not yet reached, and it shows: the countdown to rush tickets ××day ×× hour ×× minutes ×× seconds

2) Start buying, showing: there are still ×× days, ×× minutes, ×× seconds left in ××

3) The rush purchase is completed, showing: This round of rush purchase is over

The start time and deadline of the snap-up purchase can be output through the C# code.

This code is compatible with other browsers.

<div > 
 </div> 
 <br /> 
 <br /> 
 <br /> 
 <br /> 
 <mce:script type="text/javascript"><!-- 
 //Ticket grab start time: 10:10:00 on February 29, 2011. The definition of month starts from 0, so 1 represents February. var start = new Date('2011', '1', '29', '10', '10', '00'); 
 //var start = new Date('2011','1', '28', '15', '10', '00'); 
 
 //Ticket grab deadline 11:10:59, February 29, 2011 var expire = new Date('2011', '1', '29', '11', '10', '59'); 
 
 //Display countdown HTML object var clock = ('clientclock'); 
 
 //Countdown function, executed once per second setLeftTime(); 
 
 function setLeftTime() { 
 
 var now = new Date(); 
 
 //If the current time is less than the start time of ticket grabbing, then it is displayed: the countdown to ticket grabbing ××day ×× hour ×× minutes ×× seconds if (now < start) { 
 var diff = -480 - () //It is the time difference between Beijing time and local time var lefttime = (() - ()) + diff * 60000 
 var day = (lefttime / (1000 * 60 * 60 * 24)); 
 var hour = (lefttime / (1000 * 3600)) - (day * 24); 
 var minute = (lefttime / (1000 * 60)) - (day * 24 * 60) - (hour * 60); 
 var second = (lefttime / (1000)) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60); 
 
  = 'Countdown to grab tickets:<span class="ztCopy">' + day + '</span>sky <span class="ztCopy">' + hour + '</span>hour <span class="ztCopy">' + minute + '</span>point <span class="ztCopy">' + second + '</span>Second'; 
 } 
 //If the current time is greater than or equal to the start time of the ticket grabbing and less than the deadline for the ticket grabbing, that is, the ticket grabbing is being snatched, then it is displayed: there are still ×× days ×× hours × minutes ×× seconds left in the ticket grabbing else if (now <= expire) { 
 var diff = -480 - () //It is the time difference between Beijing time and local time var lefttime = (() - ()) + diff * 60000 
 var day = (lefttime / (1000 * 60 * 60 * 24)); 
 var hour = (lefttime / (1000 * 3600)) - (day * 24); 
 var minute = (lefttime / (1000 * 60)) - (day * 24 * 60) - (hour * 60); 
 var second = (lefttime / (1000)) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60); 
 
  = 'There are still tickets left:<span class="ztCopy">' + day + '</span>sky <span class="ztCopy">' + hour + '</span>hour <span class="ztCopy">' + minute + '</span>point <span class="ztCopy">' + second + '</span>Second'; 
 } 
 //If the current time is greater than the deadline for ticket grabbing, that is, this round of ticket grabbing ends, then it is displayed: this round of ticket grabbing ends else { 
  = 'This round of buying ends'; 
 } 
 
 setTimeout("setLeftTime()", 1000); 
 } 

// --></mce:script>

For more articles about countdown, please check out the topic:Countdown function

For more JavaScript clock effects, click to view:JavaScript clock special effects topic

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.