I’m trying to have my backbone application check the server as often as possible for updates to a model, similar to how twitter’s site has new tweets that are automatically added.
My current setup is checking an external application through their api so I have no access to their server which leaves me to rely on the client side to do the checking without being too memory hungry, how can I achieve this?
In Javascript the only way you can really control timing is through setTimeout/setInterval; there is no “more sophisticated” mechanism, unless you count helper functions (eg. ‘delay’) which just wrap setTimeout/setInterval.
So, dmi3y’s answer was correct. However, since you mentioned Backbone in both the tags and in the description, here’s a more Backbone-ish version…
or, if you wanted to build it in to your class …
It’s also worth mentioning that setInterval returns an object which you can pass to clearInterval if you want to stop “polling”.
P.S. Just in case you’re not familiar with
_.bind, it comes from the Underscore library, which Backbone depends on so you already have it. All it does is fixthisin place, so that when your timeout/interval function resolves, thethisinside it will be the second argument to_.bind(and notwindow, which is what it would normally be).