0

I Have one Date variable, and I am updating it every second to make it live.

Now here is my variable.

var stationdate = new Date(data.localTime);

My Javascript code to update it per second.

window.setInterval(function () {        
    stationdate = new Date(stationdate.setSeconds(stationdate.getSeconds() + 1));            
  }, 1000);

And My Type Script code to return it to Angular UI.

window.setInterval(() => this.time = stationdate, 1000);

My Issue.

It works perfectly if both functions are seprate.

but it stops working if I combine them.

see below.

window.setInterval(function () {        
    stationdate = new Date(stationdate.setSeconds(stationdate.getSeconds() + 1));
    this.time = stationdate;            
  }, 1000);

AM I MISSING SOMETHING WITH FAT ARROW OF TYPE SCRIPT?

What should be the proper function?

1

5 Answers 5

5

Each function has it's own this context. So your this refers to another object in the function expression. With arrow function it refers to the outer this which contains that arrow function.

Use arrow syntax and there is no necessary to use window prefix behind the setInterval.

setInterval(() => {        
    stationdate = new Date(stationdate.setSeconds(stationdate.getSeconds() + 1));
    this.time = stationdate;            
  }, 1000);

For more read Arrow function vs function declaration/expression - this is one of the main concepts in Javascript.

Sign up to request clarification or add additional context in comments.

1 Comment

@Bharat Read that SO answer and you will understand
1

In your combination of both function your not using an arrow function, which leads to the context of this is window. Use an arrow function to keep the outer context of this.

window.setInterval(() => {        
    stationdate = new Date(stationdate.setSeconds(stationdate.getSeconds() + 1));
    this.time = stationdate;            
}, 1000);

Comments

1

In your second example, this refers to the inner-scope of your function not the outer scope where your variable is most likely defined. Therefore as others have suggested, you need to use the lambda symbol.

Also, since you're using AngularJS it would be preferable to use $interval instead of window.setInterval().

Note that by using $interval, a $scope.$apply() will be made to do dirty-checking if your variable has changed and then update the UI. This is not a given when using window.setInterval(). Alternatively, you can still tell $interval to not use $apply if that is something you need.

2 Comments

I am using Angular 4.3, and It seems $interval is not easy to use in 4.3.
@Bharat, ahh judging by the usage of ES5 and the tag 'AngularJS' I assumed you were using AngularJS 1.x.
1

It common bug of using callbacks. All in javascript has own 'this' context (except arrow functions). Just for systems function this===undefined.

So proper way is use arrow function (as it shown above). Best for modern browsers (or transpiled code).

 window.setInterval(() => {        
    stationdate = new Date(stationdate.setSeconds(stationdate.getSeconds() + 1));
    this.time = stationdate;            
  }, 1000);

Another way to store 'this' is binding context:

window.setInterval((function () {        
    stationdate = new Date(stationdate.setSeconds(stationdate.getSeconds() + 1));
    this.time = stationdate;            
  }).bind(this), 1000);

Or more explanation:

window.setInterval(
    (function(){console.log(this.hello);}).bind({hello:'hello!'})
   , 1000);

Comments

1
window.setInterval( () => {        
    stationdate = new Date(stationdate.setSeconds(stationdate.getSeconds() + 1));
    this.time = stationdate;            
  }, 1000);

In your code this has wrong context. The fat arrow function is feature of ES6 and has nothing to do with TypeScript

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.