Harshwardhan Sharma
Harshwardhan Sharma

Reputation: 261

Why server time become freez in javascript

When i want to get the server time in javascript conitnuously why it become freez

setInterval(function() {
    var server_now = <?php echo time(); ?> * 1000;
    console.log("server_now = "+server_now);
})

why it result only

server_now = 1511933318000

Why it does not continuously update

Upvotes: 1

Views: 26

Answers (2)

Razon Yang
Razon Yang

Reputation: 454

php script only parse once per request, the js variable server_now is always same.

var time = <?=time()?>;

console.log(time);

function increment() {
    time += 10;
    console.log(time);
}

setInterval("increment()", 10 * 1000);

But if you would like to acquire the exact server time, there should be a server API for that purpose, and then use ajax to invoke that API.

Upvotes: 1

laiju
laiju

Reputation: 1373

The php echo time will only update on page reload. It won't update using setInterval.

Upvotes: 1

Related Questions