Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2008
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Ajax call after success function

    This problem is solved and no longer needs any help.

    I have this code and am experiencing some problems
    Code:
    $.ajax({
    		url: this.html_url,
    		cache: false,
    		success: function(html){
    		      $('body:last-child').append(html);
    		      return true;
    		}
    	});
    doSomething();
    My problem is that doSomething is called before the ajax success function has completed. I would like doSomething to be called immediately after the ajax call is completed including the success function. Any help?
    Last edited by mberkom; 03-31-2009 at 02:06 AM. Reason: Problem Solved

  • #2
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    If memory serves me right... the success part of jQuery's ajax is a callback, so....

    Code:
    $.ajax({
    	url: this.html_url,
    	cache: false,
    	success: function(html){
    		$('body:last-child').append(html);
    		doSomething();
    		return true;
    	}
    });
    Should achieve the desired result.
    The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

    How to ask smart questions?

  • #3
    New Coder
    Join Date
    May 2008
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    For various reasons I am unable to do as you suggest. I am writing this code in the context of object oriented jquery and this ajax call is part of a method of the object "Box". The "doSomething" function requires access to the variables of the object which for some reason are made undefined by that ajax call. This caused me trouble for a while until I randomly put "return true;" in the success function. This solved my inability to access the object variables but then stopped me from being able to call my "doSomething" function in the success part of the ajax call. When I call it outside of the ajax then it executes before the ajax and is unable to access any of the newly loaded html. Do you see my problem? If not I may spend some time putting all my files up here and a detailed explanation of what I am trying to create.

  • #4
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    I guess you could try the following:

    Code:
    $('body:last-child').bind('ajaxSuccess', function() {
    	doSomething();
    })
    Should probably work, ajaxSuccess being a global event, but eh... other than that... JS will always first complete everything in the first level of actions before going deeper, unless what you need to do is linked to a callback of sorts.
    The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

    How to ask smart questions?


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •