Hi Safraz, can you provide an example on jsbin or somewhere else where setInterval "keeps on calling itself irrespective of whether or not the callback function completed." ? i kind of knew it, but never been able to replicate it.
cheers, jgabios http://bash.editia.info On Fri, Jan 14, 2011 at 12:10 PM, SARFRAZ AHMED <sarfraznawaz2...@gmail.com> wrote: > 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 <sarfraznawaz2...@gmail.com> 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 <foldifoldi.vi...@gmail.com> 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/jsmentors@jsmentors.com/ > > To search via a non-Google archive, visit here: > http://www.mail-archive.com/jsmentors@googlegroups.com/ > > To unsubscribe from this group, send email to > jsmentors+unsubscr...@googlegroups.com > -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com