Solved

make jquery to wait for .get function

Posted on 2011-09-09
5
541 Views
Last Modified: 2012-05-12
Hello.

I have this function
function runAjaxCap(mycode) {
	//$("#captchacatcher")
	var xaa='';
	xaa='parser3.php?code='+mycode;	
	jQuery.get(xaa,function(fileContents){jQuery("#captchacatcher").html(fileContents);});

}

Open in new window


I need to wait until
jQuery.get(xaa,function(fileContents){jQuery("#captchacatcher").html

Open in new window

has change my div content.

Please HELP.
Thanks
0
Comment
Question by:radugheorghies
  • 2
  • 2
5 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
use $.ajax (or use jQuery.ajaxSetup) to make a synchrone ajax call :

function runAjaxCap(mycode) {
	//$("#captchacatcher")
	var xaa='';
	xaa='parser3.php?code='+mycode;	
	var fileContents = jQuery.ajax({ "url":xaa "async":false }).responseText;
        jQuery("#captchacatcher").html(fileContents); 
}

Open in new window


more info : http://arshadinfo.wordpress.com/2008/05/25/what-is-the-difference-between-asynchronous-ajax-and-synchronous-request/
0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
Comment Utility
try:
function runAjaxCap(mycode) {
//force it to wait for get completion
jQuery.ajaxSetup({async:false});
	//$("#captchacatcher")
	var xaa='';
	xaa='parser3.php?code='+mycode;	
	jQuery.get(xaa,function(fileContents){jQuery("#captchacatcher").html(fileContents);});

//restore default so it won't "break/interfere" with other ajax calls
jQuery.ajaxSetup({async:true});

}

Open in new window

0
 

Author Comment

by:radugheorghies
Comment Utility
Sorry leakim971

something is wrong here:
var fileContents = jQuery.ajax({ "url":xaa "async":false }).responseText;

thanks for answering.
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
add a comma after xaa

var fileContents = jQuery.ajax({ "url":xaa, "async":false }).responseText;
0
 

Author Closing Comment

by:radugheorghies
Comment Utility
Thank you.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now