Hi,

All you need to do is make sure you have a reference to the PE, so you
can call its stop function. The PE will pass a reference to itself
into the function it's calling, so you can do this with arguments. Or
you can just keep a reference to the PE when you create it, and later
use that reference to stop it.

Option 1: Use the argument the PE passes you:
* * * *
// No need for the extra wrapper function, the PE can call `checkAll`
directly
// and doing so gives us access to the argument
//WAS: new PeriodicalExecuter(function() { checkAll() },30);
new PeriodicalExecuter(checkAll, 30);

// `checkAll` now gets the PE instance as an argument
//WAS: function checkAll(){
function checkAll(pe){
    for (i=1; i<=6; i++){
        // pass it on to `check`
        check(i, pe);
    }
}

// `check` now receives the PE instance as an argument
//WAS: function check(num){
function check(num, pe){
    $('jetway0'+num).value ='...';
    $('jetway0'+num).style.color='black';
    new Ajax.Request("http://someIP:80"+num+"/somefile.jsp";, {
        onSuccess: function(transport) {
            if (transport.status == 200) {
                $('jetway0'+num).value ='Up';
                $('jetway0'+num).style.color='green';
            }else{
                $('jetway0'+num).value ='Code: '+transport.status;
                $('jetway0'+num).style.color='olive';
            }
        },
        onFailure: function(){
            $('jetway0'+num).value='Down';
            $('jetway0'+num).style.color='red';
            // Stop the PE
            pe.stop();
        }
    });
}
* * * *

Option 2: Remember a reference when you create it (I wouldn't create a
new global variable, though, which is what this does if all of this is
at the top level of the file):
* * * *
// Remember a reference to the PE we're creating
//WAS: new PeriodicalExecuter(function() { checkAll() },30);
var theAjaxPE = new PeriodicalExecuter(function() { checkAll() },30);
// Or, again, there's no need for that wrapper function, could be:
//var theAjaxPE = new PeriodicalExecuter(checkAll, 30);

function checkAll(){
    for (i=1; i<=6; i++){
        check(i);
    }
}

function check(num){
    $('jetway0'+num).value ='...';
    $('jetway0'+num).style.color='black';
    new Ajax.Request("http://someIP:80"+num+"/somefile.jsp";, {
        onSuccess: function(transport) {
            if (transport.status == 200) {
                $('jetway0'+num).value ='Up';
                $('jetway0'+num).style.color='green';
            }else{
                $('jetway0'+num).value ='Code: '+transport.status;
                $('jetway0'+num).style.color='olive';
            }
        },
        onFailure: function(){
            $('jetway0'+num).value='Down';
            $('jetway0'+num).style.color='red';
            // Stop the PE
            theAjaxPE.stop();
        }
    });
}
* * * *

HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com


On May 28, 4:11 pm, "Russell Keith" <russell.ke...@aacreditunion.org>
wrote:
> I have a PeriodicalExecuter that is calling a function every so often
> that makes several AJAX calls.  I am not having any luck getting the
> PeriodicalExecuter to stop via the onFailure callback in the Ajax call.
> Is this possible?  I see in the API doc that I can use stop() within the
> PeriodicalExecuter to stop it, but how can I do this outside?
>
> new PeriodicalExecuter(function() { checkAll() },30);
>
> function checkAll(){
>
>     for (i=1; i<=6; i++){
>
>         check(i);
>
>     }
>
> }
>
> function check(num){
>
>     $('jetway0'+num).value ='...';
>
>     $('jetway0'+num).style.color='black';
>
>     new Ajax.Request("http://someIP:80"+num+"/somefile.jsp";, {
>
>                 onSuccess: function(transport) {
>
>             if (transport.status == 200) {
>
>                        $('jetway0'+num).value ='Up';
>
>                $('jetway0'+num).style.color='green';
>
>                     }else{
>
>                        $('jetway0'+num).value ='Code:
> '+transport.status;
>
>                $('jetway0'+num).style.color='olive';
>
>                     }
>
>                 },
>
>         onFailure: function(){
>
>             $('jetway0'+num).value='Down';
>
>             $('jetway0'+num).style.color='red';
>
>                 ********** I want to stop the PeriodicalExecuter here
> **********
>
>         }
>
>     });      
>
> }
>
> Russell

-- 
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.

Reply via email to