Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 5 votes, 4.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-30-2009, 10:15 PM   PM User | #1
mberkom
New Coder

 
Join Date: May 2008
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
mberkom is an unknown quantity at this point
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
mberkom is offline   Reply With Quote
Old 03-30-2009, 10:40 PM   PM User | #2
Eldarrion
Regular Coder

 
Join Date: Feb 2009
Location: Wheeling, IL
Posts: 358
Thanks: 5
Thanked 62 Times in 60 Posts
Eldarrion is on a distinguished road
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?
Eldarrion is offline   Reply With Quote
Old 03-30-2009, 11:55 PM   PM User | #3
mberkom
New Coder

 
Join Date: May 2008
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
mberkom is an unknown quantity at this point
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.
mberkom is offline   Reply With Quote
Old 03-31-2009, 02:34 PM   PM User | #4
Eldarrion
Regular Coder

 
Join Date: Feb 2009
Location: Wheeling, IL
Posts: 358
Thanks: 5
Thanked 62 Times in 60 Posts
Eldarrion is on a distinguished road
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?
Eldarrion is offline   Reply With Quote
Reply

Bookmarks

Tags
ajax call, jquery

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:44 PM.


Advertisement
Log in to turn off these ads.