JavaScript Merchandise Snapping Countdown

Source: Internet
Author: User
Tags local time

Full Merchandise snapping Countdown

Snapping countdown is not possible with the new date () of local time, otherwise everyone sees the time is not uniform, so the time to use the server as the initial value

Ideas:

1, Time Unified Use server time (Get server time method a variety of), then local time and server time have a difference (offset) to determine the time for each computer is unified

2, use start time, end time and local time subtract, get to time difference, plus is offset (here time is converted to timestamp for calculation)

JS section:

Get server time start-to-

var xhr = new XMLHttpRequest ();

if (!XHR) {

XHR = new ActiveXObject (' microsoft.xmlhttp ');

}

Xhr.open (' Get ', '/', false);

Xhr.setrequestheader (' if-modified-since ', ' Q ');

Xhr.send ();

var timerstr = xhr.getresponseheader (' Date ');

var servertime = new Date (TIMERSTR);

var dateTime = new Date ();

Get server time End--

Defining functions

function Gtime () {

var nowtime = new Date ();

var difference = datatime.gettime ()-servertime.gettime (); Offset of local time and service time

var startTime = new Date (' 2017/06/01 19:00:00 ')//time is a timestamp given by the background and then converted to the format of the new date output

var endTime = new Date (' 2017/06/30 19:00:00 ')//time is a timestamp given by the background and then converted to the format of the new date output

var lefttime = parseint ((Starttime.gettime ()-nowtime.gettime () + difference)/1000

var onTime = parseint ((Endtime.gettime ()-nowtime.gettime () + difference)/1000

var d = parseint (LEFTTIME/3600/24);

var h = parseint ((lefttime/3600)%24);

var m = parseint ((LEFTTIME/60)%60);

var s = parseint (lefttime%60);

  

var D2 = parseint (ONTIME/3600/24);

var H2 = parseint ((ontime/3600)%24);

var m2 = parseint ((ONTIME/60)%60);

var s2 = parseint (ontime%60);

if (Lefttime > 0) {

document.getElementById ("Lefttime"). InnerHTML = "distance activity starts with:" + "+" <span> "+ D +" </span> Day <span> "+ H + "</span> hours <span>" + M + "</span> minutes <span>" + S + "</span> seconds";

}else if (OnTime > 0) {

document.getElementById ("Lefttime"). InnerHTML = "From the end of the event there are:" + "+" <span> "+ d2 +" </span> days <span> "+ H 2 + "</span> hours <span>" + m2 + "</span> points <span>" + s2 + "</span> seconds";

}else if (OnTime < 0) {

document.getElementById ("Lefttime"). InnerHTML = "Activity has ended, please continue to follow our news";

Clearinterval (BeginTime);

}

}

Gtime ();

var beginTime = setinterval (gettime,1000)

HTML section

<div id= "Lefttime" ></div>

JavaScript Merchandise Snapping Countdown

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.