Phil Jackson
Phil Jackson

Reputation: 10288

jQuery - stop function

what im wanting to achieve is if a error appears ( one which I have come up with ) and contains the word 'error' then the function stops in its tracks. What the function basically consists of is 5 ajax requests, when one is successful the other kicks in.. like so..

function thing() {
    $.ajax({
        ...
        ...  
        success: 
        function(html){
            errorGet(html);   
            $.ajax({
            ...
            ...  
            success: 
            function(html){
                errorGet(html);
                    ...
                ... 

I have a function in place 'errorGet()' to try and stop the function;

function errorGet(data){
    var re = /error/mgi;
    if( data.search( re ) == 0 ){
        thing.stop;
    }
}

but i know thing.stop doen't work but does anyone know how to stop the process?

Upvotes: 2

Views: 2532

Answers (2)

Alpha Codemonkey
Alpha Codemonkey

Reputation: 3261

You could have a function that calls your AJAX requests without being asynchonous. That way that don't have to be netsed, and you can just return to exit the function.

function thing() {
    var stop = false;

    $.ajax({
        async: false,
        ...  
        success: 
        function(html){
            stop = errorGet(html);
        }
    }

    if(stop) {
        return false;
    }

    $.ajax({
        async: false,
        ...  
        success: 
        function(html){
            stop = errorGet(html);
        }
    }
}

function errorGet(data){
    var re = /error/mgi;
    if( data.search( re ) == 0 ){
        return false; //no error
    }
    return true;
}

Upvotes: 5

Pointy
Pointy

Reputation: 414086

Have you tried throwing an exception?

throw "there was an error so I am going to stop";

Upvotes: 2

Related Questions