views:

318

answers:

3

Hi,

I have a tightly coupled javascript, where in there are series of if-else checks and multiple ajax calls are made. The ajax calls are nested type. My problem is I am in a deep nested ajax callable function and I want to get out from there gracefully.

The snippet of the code is .

function showSubscriptionLightBox() {

$.get("/ajax/get_subscription_lightbox_content.php?feed_id=" + feedid, function(data) {

//Work on the data we receive... and check whether user is logged in.

if(userLoggedIn) {

//Make one more ajax call

$.get("/ajax/is_user_subscribed.php?feed_id=" + feedid, function(data) {

//Work on data again.... and check if user is subscribed.

if(userSubscribed) {

//Then there is popup which comes up, a part of same page and it has a button name "task".
document.getElementById('task').onclick = function() {

if(document.getElementById('email_mode').checked) {
$.ajax({
url : "ajax/is_user_email_verified.php?user_id="+userID,
success : function(data) {
  if(!data)
    return;

  var response;
    response = eval("response = " + data);

     if(!response)
         return;

     if(response['email_status'] == 0) {
        //Exit from here 
}}}

...... other part of code..

I want to exit gracefully from javascript, when the response['email_status'] == 0

Please tell me, how to do this??

I tried the return statement, but it took me to the enclosing function and not outside the script.

Thanks,

Amit

A: 

There's a funny trick you can always use in JavaScript to escape the call stack: setTimeout(). It's useful in many situations, not just this, it is often used to work around DOM event related bugs in browsers as well.

$.ajax(
{
   url: 'lol.php',
   success: function(data)
   {
       setTimeOut(function()
       {
            // Your code comes here
       }, 0); // 0 will ensure that it gets executed immediately
   }
});
DrJokepu
A: 

I know that with Prototype you could do this with try/catch blocks. You could throw an object from within one of the inner functions and it will travel up the call stack for other functions to intercept.

Josh
A: 

For what it is worth, here is some code from one of my applications. It syncs records using JSONP and AJAX. It first gets an array of object ids from a remote server. It then fetches the record for the object id at the zero index from the host server. Then it sends the record it receives to the remote server. At that point, it continues the process by starting the process with an incremented index into the array of ids. It terminates when the index reaches the end of the array.

(function( $ ) {
    $.getJSON( 'http://remote.com/admin/record_ids.js?callback=?', function( data ) {
        var set_record = function( index ) {
            if ( index < data.length ) {
                $.get( 'record_get.json', { contact_id: data[ index ] }, function( data ) {
                    $.getJSON( 'http://remote.com/admin/record_save.js?callback=?', data, function() {
                        set_record( index + 1 );
                    });
                }, 'json');
            }
        };
        set_record( 0 );
    });
})( jQuery );

As you can see, when you want to get out gracefully, you just don't call. I can't imagine why you can't just return to stop your code.

Alan Gutierrez