Javascript implements flash sales and shares records of group buying countdown display

Source: Internet
Author: User

Recently, I made a flash sale for a real estate. The e-commerce website for group purchases (there are also flash sales in the House) has a countdown to flash sales, it mainly determines how long the current time is from the start of the second kill, and displays the start and end of the second kill.
The most important point is that the current time cannot be obtained by the browser through the new Date () Client time, so that as long as the user modifies his machine time, the countdown will be messy, therefore, the current time must use the server time, so the static cache page is used. Therefore, the current time is obtained using ajax.

Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml" lang = "zh-CN">
<Head>
<Title> </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Meta http-equiv = "Content-Language" content = "zh-CN"/>
<Meta http-equiv = "X-UA-Compatible" content = "IE = EmulateIE7"/>
<Meta content = "all" name = "robots"/>
<Meta name = "author" content = ""/>
</Head>
<Body onload = 'start () '>
.
From the beginning of the activity: <span id = "sk_time"> </span> <! -- Display the countdown -->
.
<Br/>
<Span id = "wyz">
<Span class = "btn_02"> participate in seckilling !!! </Span> <! -- This is the second kill button. When the countdown is 0, it will become a second kill style. -->
</Span>
.
<Script type = "text/javascript">
Var msbegintime = "1323446400000"; // The start timestamp of the activity.
Var msendtime = "1325174400000"; // This is the end time stamp of the activity.
Function start (){
CallBackServerTime ("sk_time", "wyz", msbegintime, msendtime );
}
// _ Showtimediv: time display area; _ showqdiv: Status display area
// This sends an ajax request to the server. The server returns the current timestamp of the server, that is, xmlobj. responseText is the timestamp of the server.
Function callBackServerTime (_ showtimediv, _ showqdiv, _ ms_begintime, _ ms_endtime ){
Var now = new Date ();
Var urlstr = "random =" + Math. round (Math. random () * 10000000 );
Var ajaxobj = new AJAXRequest; // create an AJAX object
Ajaxobj. method = "GET"; // set the request method to GET
Ajaxobj. url = "/gz/source/getServerTime. do? "+ Urlstr; // pay attention to cross-origin ajax issues
Ajaxobj. callback = function (xmlobj ){
// ShowQTime (xmlobj. responseText, _ showtimediv, _ showqdiv, _ ms_begintime, _ ms_endtime, _ tryid, sourceid );
ShowQTime (_ showtimediv, _ showqdiv, "1323158067288", _ ms_begintime, _ ms_endtime); // replace xmlobj. responseText with a static number for testing.
}
Ajaxobj. send (); // send the request
}
// Dynamically display the "second kill" Time Function
Function ShowQTime (_ showtimediv, _ showqdiv, _ nowtime, _ ms_begintime, _ ms_endtime ){
_ Nowtime = Number (_ nowtime );
Var timmer = Math. floor (_ ms_endtime-_ nowtime)/(1000 ));
If (_ nowtime >=_ ms_begintime & timmer> 0 ){;
// The second kill is in progress
Document. getElementById (_ showtimediv ). innerHTML = "<span class = 'pim _ time'> 0 </span> days <span class = 'pim _ time'> 0 </span> hours <span class =' pim _ time'> 0 </span> minutes <span class = 'pim _ time'> 0 </span> seconds ";
Document. getElementById (_ showqdiv ). innerHTML = "<span class = 'btn _ 01 '> <a href ='/gz/sk/v/'> the second kill has started !!! </A> </span> ";
} Else {
// Countdown to the second kill
Var nMS = _ ms_begintime-_ nowtime; // calculate the difference between the start time and the current time.
Var nD = Math. floor (nMS/(1000*60*60*24 ));
Var nH = Math. floor (nMS/(1000*60*60) % 24;
Var nM = Math. floor (nMS/(1000*60) % 60;
Var nS = Math. floor (nMS/1000) % 60;
Var nMS = Math. floor (nmilliseconds/100) % 10;
If (nD> = 0 ){
Var _ timestr = "";
Var snd = nD. toString ();
If (snd. length = 1 ){
Snd = "0" + snd;
}
_ Timestr + = "<span class = 'pim _ time'>" + snd. substring (0, 1) + snd. substring (1, 2) + "</span> day ";
Var snH = nH. toString ();
If (snH. length = 1 ){
SnH = "0" + snH;
}
_ Timestr + = "<span class = 'pim _ time'>" + snH. substring (0, 1) + snH. substring (1, 2) + "</span> Hour ";
Var snM = nM. toString ();
If (snM. length = 1 ){
SnM = "0" + snM;
}
_ Timestr + = "<span class = 'pim _ time'>" + snM. substring (0, 1) + snM. substring (1, 2) + "</span> minute ";
Var snS = nS. toString ();
If (snS. length = 1 ){
SnS = "0" + snS;
}
_ Timestr + = "<span class = 'pim _ time'>" + snS. substring (0, 1) + snS. substring (1, 2) + "</span> seconds ";
Document. getElementById (_ showtimediv). innerHTML = _ timestr;
} Else {
// The second kill is over
Document. getElementById (_ showtimediv ). innerHTML = "<span class = 'pim _ time'> 0 </span> days <span class = 'pim _ time'> 0 </span> hours <span class =' pim _ time'> 0 </span> minutes <span class = 'pim _ time'> 0 </span> seconds ";
Document. getElementById (_ showqdiv ). innerHTML = "<span class = 'btn _ 01 '> <a href ='/gz/sk/v/'> the second kill is over !!! </A> </span> ";
}
}
// Note (_ nowtime + 1000) increase by 1 second
SetTimeout ("ShowQTime ('" + _ showtimediv + "', '" + _ showqdiv + "', '" + (_ nowtime + 1000) + "', '"+ _ ms_begintime +"', '"+ _ ms_endtime +"') ", 1000 );
}
Function AJAXRequest (){
Var xmlObj = false;
Var CBfunc, ObjSelf;
ObjSelf = this;
Try {xmlObj = new XMLHttpRequest ;}
Catch (e ){
Try {xmlObj = new ActiveXObject ("MSXML2.XMLHTTP ");}
Catch (e2 ){
Try {xmlObj = new ActiveXObject ("Microsoft. XMLHTTP ");}
Catch (e3) {xmlObj = false ;}
}
}
If (! XmlObj) return false;
This. method = "POST ";
This. url;
This. async = true;
This. content = "";
This. callback = function (cbobj) {return ;}
This. send = function (){
If (! This. method |! This. url |! This. async) return false;
XmlObj. open (this. method, this. url, this. async );
If (this. method = "POST") xmlObj. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
XmlObj. onreadystatechange = function (){
If (xmlObj. readyState = 4 ){
If (xmlObj. status = 200 ){
ObjSelf. callback (xmlObj );
}
}
}
If (this. method = "POST") xmlObj. send (this. content );
Else xmlObj. send (null );
}
}
</Script>
</Body>
</Html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.