Jump to content

jQuery- chaining events?


End User

Recommended Posts

I have a function in jQuery that does an ajax call and returns a menu bar. After the menu bar is returned it needs to be initialized (or reinitialized, as the case may be). Currently I'm using this code, but I know this isn't the "proper" way to do it:

function makeMenu(menuid){$.ajax({	type: 'POST',	url: 'function_handler.php',	data: {act: 'make_menu', menu_id: menuid},	cache: false, 	success: function(data){		$('#menudiv').html(data);	}});	setTimeout("initToolbar()",500);	}

I have to wait a short period of time after the ajax call before I can successfully initialize the menu bar. I know that using 'setTimeout' isn't the correct way to do this, but I'm unsure of how to properly chain these two events together so that the initToolbar() function will always occur after the menu bar function has finished. How would I chain these two events so that initToolbar() is always run after makeMenu() has finished getting the menu bar? Is the following code the correct way to do this?

function makeMenu(menuid){$.ajax({	type: 'POST',	url: 'function_handler.php',	data: {act: 'make_menu', menu_id: menuid},	cache: false, 	success: function(data){		$('#menudiv').html(data);		initToolbar();	}});	}

This code seems to work, but I guess I'm asking if a) this is the proper way to do it, and :) in the code above, will the initToolbar() function always wait until $('#menudiv').html(data) has completed?

Link to comment
Share on other sites

A ) Not sure... to me, everything in jQuery seems like a quick&dirty solution as opposed to a "proper" one.B ) Yes.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...