JavaScript implementation seconds kill, Group buy countdown show record sharing _javascript tips

Source: Internet
Author: User

Recently made a real estate for a second kill, group buying E-commerce site (house also has seconds to kill, shot is not small AH), which has a second kill Countdown show, mainly to determine the current time distance seconds to kill how much time, there are seconds to kill start and second kill the end of a variety of display.
The main point is that the so-called current time can not use the browser through new Date () to obtain the client time, so long as the user changes their own machine time then the countdown will be chaotic, so this current time must use the server time, So we're using a static cache page, so this is the current time to get the Ajax way.

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<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= ""/>
<body onload= ' Start () ' >
.
From activity start also: <span id= "Sk_time" ></span> <!--this is the countdown display-->
.
<br/>
<span id= "Wyz" >
<span class= "btn_02" > Participate in the second kill!!! </span> <!--This is a second kill button, the countdown to 0 will become a second kill style-->
</span>
.
<script type= "Text/javascript" >
var msbegintime = "1323446400000"; This is the time stamp for the start of the activity.
var msendtime = "1325174400000"; This is the time stamp for the end 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, and the server returns the current timestamp of the server, that is, Xmlobj.responsetext is a server timestamp
function Callbackservertime (_showtimediv, _showqdiv, _ms_begintime, _ms_endtime) {
var now = new Date ();
var urlstr = "random=" + Math.Round (math.random () * 10000000);
var ajaxobj = new Ajaxrequest; Creating Ajax Objects
Ajaxobj.method = "Get"; Set the request mode to get
Ajaxobj.url = "/gz/source/getservertime.do" + urlstr; Notice the Cross-domain problem with Ajax
Ajaxobj.callback = function (xmlobj) {
Showqtime (Xmlobj.responsetext, _showtimediv, _showqdiv, _ms_begintime, _ms_endtime, _tryid,sourceid);
Showqtime (_showtimediv, _showqdiv, "1323158067288", _ms_begintime, _ms_endtime); This uses static numbers instead of Xmlobj.responsetext for easy testing.
}
Ajaxobj.send (); Send Request
}
Dynamic display of "seconds 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) {;
Second Kill in progress
document.getElementById (_showtimediv). InnerHTML = "<span class= ' pim_time ' >0</span> days <span ' PIM _time ' >0</span> hours <span class= ' pim_time ' >0</span> minutes <span class= ' Pim_time ' >0</span > sec ";
document.getElementById (_showqdiv). InnerHTML = "<span class= ' btn_01 ' ><a href= '/gz/sk/v/' > second kill started!!! </a></span> ";
} else {
Seconds to kill the countdown
var NMS = _ms_begintime-_nowtime; To calculate the time stamp 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 (nms/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> minutes";
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> sec";
document.getElementById (_showtimediv). InnerHTML = _timestr;
}else {
The second kill is over.
document.getElementById (_showtimediv). InnerHTML = "<span class= ' pim_time ' >0</span> days <span ' PIM _time ' >0</span> hours <span class= ' pim_time ' >0</span> minutes <span class= ' Pim_time ' >0</span > sec ";
document.getElementById (_showqdiv). InnerHTML = "<span class= ' btn_01 ' ><a href= '/gz/sk/v/' > second kill ended!!! </a></span> ";
}
}
Note (_nowtime + 1000) increase by 1 seconds
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>

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.