get time from server and run it from there in javascript

Solutions on MaxInterview for get time from server and run it from there in javascript by the best coders in the world

showing results for - "get time from server and run it from there in javascript"
Julia
18 Jun 2019
1var serverTime = 1490856278000;
2    var expected = serverTime;
3    var date;
4    var h;
5    var m;
6    var s;
7    var now = performance.now();
8    var then = now;
9    var dt = 0;
10    var nextInterval = (interval = 1000);
11
12    setTimeout(step, interval);
13    function step() {
14      then = now;
15      now = performance.now();
16      dt = now - then - nextInterval;
17      nextInterval = interval - dt;
18      serverTime += interval;
19      date = new Date(serverTime);
20      h = date.getHours();
21      m = date.getMinutes();
22      s = date.getSeconds();
23
24      var session = "AM";
25
26      if (h == 0) {
27        h = 12;
28      }
29
30      if (h > 12) {
31        h = h - 12;
32        session = "PM";
33      }
34
35      h = h < 10 ? "0" + h : h;
36      m = m < 10 ? "0" + m : m;
37      s = s < 10 ? "0" + s : s;
38
39      var time = h + ":" + m + ":" + s + " " + session;
40
41      document.getElementById("NowTime").innerHTML = time;
42
43      now = performance.now();
44
45      setTimeout(step, Math.max(0, nextInterval));
46    }