[Proto-Scripty] Re: PeriodicalExecuter counter

2010-12-23 Thread Neaox
Hi,

Below is a patch to PeriodicalExecuter that should do what your
asking, I have Prototype 1.7.min and this patch is untested:

var
PeriodicalExecuter=Class.create({initialize:function(callback,frequency)
{this.callback=callback;this.frequency=frequency;this.currentlyExecuting=false;this.iterations=0;this.registerCallback()},registerCallback:function()
{this.timer=setInterval(this.onTimerEvent.bind(this),this.frequency*1000)},execute:function()
{this.callback(this)},stop:function(){if(!
this.timer)return;clearInterval(this.timer);this.timer=null},onTimerEvent:function()
{if(!this.currentlyExecuting)
{try{this.currentlyExecuting=true;this.iterations+
+;this.execute();this.currentlyExecuting=false}catch(e)
{this.currentlyExecuting=false;throw e);

I suggest you make a backup of your current prototype implementation
if you have made any other modifications to it.

Replace the current PeriodicalExecuter class with the one above.

To find out what iteration PE is on just call 'pe.iterations', 'pe'
being the variable name of the argument supplied to the callback
function.

Or to find out from outside of the callback just use 'pe.iterations',
in this case 'pe' being the variable name in which the implementation
of PeriodicalExecuter is stored.

Hope this helps.

Cheers,

Chris

Oh and Merry Christmas.

On Dec 18, 11:20 am, JoJo tokyot...@gmail.com wrote:
 What's the best way to find which iteration the PeriodicalExecutuer is
 currently on? What I'm trying to do is step through an array slowly
 (period of 0.5 seconds) and having the ability to stop at an arbitrary
 time. The PeriodicalExecuter has the ability to stop, but it doesn't
 have the ability to step sequentially through an array. Or should I
 not even attempt to use PE's for this?

-- 
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.



[Proto-Scripty] Re: PeriodicalExecuter counter

2010-12-17 Thread T.J. Crowder
Hi,

You *can* use PeriodicalExecutor for this. I'm not sure it buys you
much:

function loopThroughArray(array, interval, callback) {
var index = 0;

setTimeout(process, interval);

function process() {
var rv;

if (index  array.length) {
try {
rv = callback(array[index++]);
}
catch (e) {
}
if (rv !== false) {
setTimeout(process, interval);
}
}
}
}

Usage:

loopThroughArray([3, 2, 1, 0, -1, -2], 500, function(entry) {
display(entry);
return entry != 0;
});

Output:

3
2
1
0

Note it stops at zero, because the callback returned `false`.

You can do much the same with PeriodicalExecutor, or with setInterval.
I prefer re-scheduling the setTimeout because it's harder to lose
control of them through stoopid bugs involving forgetting to stop
them...

FWIW,
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com


On Dec 17, 10:20 pm, JoJo tokyot...@gmail.com wrote:
 What's the best way to find which iteration the PeriodicalExecutuer is
 currently on? What I'm trying to do is step through an array slowly
 (period of 0.5 seconds) and having the ability to stop at an arbitrary
 time. The PeriodicalExecuter has the ability to stop, but it doesn't
 have the ability to step sequentially through an array. Or should I
 not even attempt to use PE's for this?

-- 
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.