I just forgot to mention one thing. Using setTimeout in combination
with self-invoking function works such that callback function always
finishes executing before another call time setTimeout is made :)

On Jan 14, 3:06 pm, SARFRAZ AHMED <[email protected]> wrote:
> You might have heard that setInterval is bad. The reason is that
> setInterval keeps on calling itself irrespective of whether or not the
> callback function completed.
>
> Well, one can mimic the setInterval using setTimeout in combination
> with self-invoking functions.
>
> Here is an example:http://jsbin.com/usali4
>
> Code Used:
>
> (function foo(){
>   alert('I will get called on page load and after 10 seconds again and
> again without setInterval :)');
>
>    setTimeout(foo, 10000);
>
> })()
>
> I had written an article on the subject that can be viewed here:
>
> http://webupon.com/audio/javascript-self-executing-functions-2/
>
> On Jan 14, 10:28 am, foldi <[email protected]> wrote:
>
> > I'm trying to understand something about setInterval. When running the
> > following in FF3, the time it takes to execute the interval's function
> > does not take longer than the 30ms specified for the interval.
> > However, it takes longer than 30ms to for the interval to run again.
> > Where is the extra time going? Is there always some overhead to
> > running a setInterval? Thanks.
>
> > Test = {};
> > Test.end_repeat = 0;
> > Test.total = 0;
>
> > Test.my_interval = setInterval(function () {
> >         Test.repeat();
>
> > }, 30);
>
> > Test.repeat = function () {
>
> >         var t;
>
> >         Test.start_repeat = new Date().getTime();
>
> >         if (Test.end_repeat != 0) {
> >                 t = Test.start_repeat - Test.end_repeat;
> >                 console.log("/////");
> >                 console.log("Repeat duration: " + t + "ms");
> >         }
>
> >         Test.end_repeat = new Date().getTime();
>
> >         //
>
> >         Test.action_start = new Date().getTime();
>
> >         for (var i = 0; i < 35000; i++) { // do something for a while; 
> > should
> > not take longer than 30ms, if so, decrease loop
> >                 t = new Date().getTime();
> >         }
>
> >         t = new Date().getTime() - Test.action_start;
> >         console.log("Action duration: " + t + "ms");
>
> >         if (Test.total++ > 10) {
> >                 clearInterval(Test.my_interval);
> >         }
>
> > }

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to