refresh date and time every second angular 1

refresh date and time every second angular

setInterval(() => {         
  //replaced function() by ()=>
  this.myDate = new Date();
  console.log(this.myDate); 
  // just testing if it is working
}, 1000);

Here is what the above code is Doing:
1. We are creating a variable called myDate and assigning it to a new Date() object.
2. We are then creating a setInterval() function that will run every second (1000 milliseconds).
3. Inside the setInterval() function, we are updating the myDate variable with a new Date() object.
4. We are then logging the myDate variable to the console.

The problem with the above code is that the this keyword is not referring to the component instance.

To fix this, we can use an arrow function.

An arrow function does not have its own this; the this value of the enclosing execution context is used.

So, in the above code, we are using an arrow function to ensure that the this keyword is referring to the component instance.

Now, let’s see how we can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the setInterval() function to update the date and time every second.

We can use the set

Similar Posts