SetTimeout vs. Ember.run.later in an ember app?

Blair Anderson picture Blair Anderson · Feb 2, 2014 · Viewed 36.5k times · Source

Inside my handlebars template:

Today's date: {{currentDate}}
Current Time: {{currentTime}}

Inside my helpers:

Ember.Handlebars.registerBoundHelper 'currentDate', (option) ->
  moment().format('LL');

Ember.Handlebars.registerBoundHelper 'currentTime', (option) ->
  moment().format('h:mm:ss a');

How would I go about updating the currentTime to the view every 1 second?

I have read the Ember recommends Ember.run.later but I can't quite figure out where to put it and how to call it using this helper.

Answer

Toran Billups picture Toran Billups · Feb 2, 2014

You can use Ember.run.later like you would normally use setTimeout

Ember.run.later((function() {
  //do something in here that will run in 2 seconds
}), 2000);

I'm not sure of the internals, but I know that integration testing Ember requires that you use run.later (if you don't the test code won't wait for the timeout to finish).