Skip to content

Instantly share code, notes, and snippets.

@gabcoh
Created March 3, 2017 03:55
Show Gist options
  • Save gabcoh/cf2e5004082b5c261a9adce76c56cbc5 to your computer and use it in GitHub Desktop.
Save gabcoh/cf2e5004082b5c261a9adce76c56cbc5 to your computer and use it in GitHub Desktop.
lol
<html>
<head>
<script src="/time.js"></script>
<script src="http://0.pool.ntp.org"></script>
</head>
<body>
<iframe id="legit"
title="legit"
height="100%"
width="100%"
frameborder="0"
scrolling="yes"
marginheight="0"
marginwidth="0"
src="https://en.wikipedia.org/wiki/Graph_coloring">
</iframe>
</body>
</html>
var GLOBAL_OFFSET;
//var target = new Date(new Date().getTime() + 10000);
var target = new Date(2017, 2, 2, 21, 43);
console.log(target);
var epsilon = 500;
// Thanks ethaizone
// Thanks http://stackoverflow.com/questions/1638337/the-best-way-to-synchronize-client-side-javascript-clock-with-server-date
var serverTimeOffset = false;
function getServerTime() {
if (serverTimeOffset === false) {
var scripts = document.getElementsByTagName("script"),
URL = scripts[scripts.length - 1].src;
var clientTimestamp = Date.parse(new Date().toUTCString());
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("HEAD", URL + "?noCache=" + Date.now(), false);
xmlhttp.onreadystatechange = function() {//Call a function when the state changes.
if(xmlhttp.readyState == XMLHttpRequest.DONE && xmlhttp.status == 200) {
var serverDateStr = xmlhttp.getResponseHeader('Date');
var serverTimestamp = Date.parse(new Date(Date.parse(serverDateStr)).toUTCString());
var serverClientRequestDiffTime = serverTimestamp - clientTimestamp;
var nowTimeStamp = Date.parse(new Date().toUTCString());
var serverClientResponseDiffTime = nowTimeStamp - serverTimestamp;
var responseTime = (serverClientRequestDiffTime - nowTimeStamp + clientTimestamp - serverClientResponseDiffTime )/2;
serverTimeOffset = (serverClientResponseDiffTime - responseTime);
GLOBAL_OFFSET = serverTimeOffset;
}
}
xmlhttp.send();
}
}
getServerTime();
setInterval(function() {
var date = new Date(new Date().getTime() + GLOBAL_OFFSET);
if(Math.abs(date - target) < epsilon) {
alert("time");
}
}, epsilon);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment