Is there a way to use setTimeout in JS binding?

I have a controller need to update data from a server each 30 seconds, but there is no way to use setTimeout in JS binding, cc.Director.getInstance().getScheduler().scheduleScriptFunc also does not work.

You can schedule updates in JS like this:

cc.Director.getInstance().getScheduler().scheduleCallbackForTarget(this, this._onTimerTick, 1, cc.REPEAT_FOREVER);

You can see the documentation here: http://www.cocos2d-x.org/reference/html5-js/symbols/cc.Scheduler.html#scheduleCallbackForTarget

This requires a CCNode instance as target node. If a I push/pop scene, the node might disposed.

My mistake, it does not require a CCNode as target.
Thanks a lot.

This method is not listed in C++ code.

James Munro wrote:

You can schedule updates in JS like this:
>
cc.Director.getInstance().getScheduler().scheduleCallbackForTarget(this, this._onTimerTick, 1, cc.REPEAT_FOREVER);
>
You can see the documentation here: http://www.cocos2d-x.org/reference/html5-js/symbols/cc.Scheduler.html#scheduleCallbackForTarget

why don’t cocos2d-x-jsb provide “global.”setTimeout and setInterval? It’s kind of de-facto standard api! :slight_smile:

Qizhi Zhang wrote:

I have a controller need to update data from a server each 30 seconds, but there is no way to use setTimeout in JS binding, cc.Director.getInstance().getScheduler().scheduleScriptFunc also does not work.