-3

I want to auto update function time() in javascript and I have write this code:

var time = null;
setInterval(function() {
      time = Date();
}, 1000);
console.log(time);

But it shows me null in console.log. I want to get auto-updated function time() in variable time

What is wrong?

John Doe
  • 29
  • 1
  • 7
  • 2
    Are you trying to update the time variable using server side logic? –  Nov 12 '17 at 17:15
  • 3
    You're logging the value to the console before you change it. You're also changing a re-declared local variable, not the global variable. You also *may* have a syntax error, depending on what that server-side code is outputting. You should examine the actual client-side code that's running. Additionally, what is this even supposed to do? You're trying to set the variable to the same value every second. Always the same value. Just set it once. – David Nov 12 '17 at 17:15
  • Use ajax to get value from ".php" file, because php runs before javascript so it will print only one value in each call : if you want to make it by js Use like : https://jsfiddle.net/nikleshraut/be64t8qe/ – Niklesh Raut Nov 12 '17 at 17:21

2 Answers2

5

Lets break down the question

  1. <?php echo time() ?> This is generated server-side and cannot be re-calculated in the client side (where your code actually runs).

  2. var time = <?php echo time() ?>; Here you are re-declaring (and by this masking) the original time variable.

  3. console.log(time); This is being called outside of the interval function scope, so it will only run once (and in that time in will print null).

You are looking for something like this :

setInterval(function(){
    console.log(Date())
}, 1000);

If you want your variable to be accessiable outside the interval's function's scope you can do something like this

var time;
setInterval(function(){
    time = Date();
    console.log(time);
}, 1000);

// Now the 'time' variable will be accessible and will hold the latest date value 
// For example console.log(time)

And of course you can replace Date() with any date/time creating function you will need for you specific purposes.

C'estLaVie
  • 263
  • 1
  • 3
  • 9
  • Yes, that I want but I need to put `Date()` as variable to use outside of setInterval function. – John Doe Nov 12 '17 at 17:26
  • @JohnDoe Do you want the 'time' variable to be accessible ? or the actual 'Date()' ? – C'estLaVie Nov 12 '17 at 17:31
  • I use this script to generate a variable `time` to the link and when user access page when i have this javascript code, he will get time in that moment, but if it stay 2, 5, or 5 seconds i want to get this last time not time when it entered on page. – John Doe Nov 12 '17 at 17:44
0

You defined new timer variable and didn't override your global variable . Also your console.log is outside of the interval scope so you print the value before it was change.

The following code work:

var time = null;
setInterval(function() {
  time = "test";
  console.log(time);
}, 1000);
Oren Shamun
  • 154
  • 12