setInterval scope problem in Object Oriented Javascript

28.12.10
I've gone through a problem while trying to use a setInterval integrated with a object oriented framework I was working on, I will briefly explain the case and solution I applied in case anyone finds it useful.
The problem was while trying to do a trigger animation function in my frame work, the function is supposed to trigger animation or stop animation depending on the parameter passed the function is setAnimation(boolean)
setAnimation(true) starts animation and triggers some action that keeps repeated in some interval (please check setInterval() function somewhere if you are not sure how it works)
The code for the above scenario in object oriented way may be summarised as below.

In this case as setInterval is using an anonymous  function it is able to call obj.changerFunction from inside it. Keep in mind that the following won't work:

15     obj.animationID=setInterval(obj.changerFunction()); 

Also taking the return ID to obj.animationID enables us to stop the animation from from any function in the object.

Related Posts by Categories



Widget by Hoctro | Jack Book

0 Responses: