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