Solved

adding callback to function

Posted on 2011-09-15
4
303 Views
Last Modified: 2012-05-12
jQuery beginner here. I know a lot of jQuery commands have callbacks, so the script won't continue until the command has finished. But can I do something like that with my own function?

Specifically, when a button is clicked on my screen, I want to do a series of things. The first is to call a function called:

savedata();

...but then I have a bunch of other jQuery commands after that. Can I make savedata(); complete BEFORE all the other commands begin?

I'm attaching a snippet of my Javascript. I am running into problems because savedata(); takes a bit of time to complete, and in the meantime, all those other commands are modifying values, so that I am not saving the right data. I need to wait until all the data is saved before continuing.

Thank you!
function icon1(){
	savedata();
	$('#icon1').attr('src', 'https://www.domain.com/art/gr_goals-icon1b.jpg');
	$('#icon2').attr('src', 'https://www.domain.com/art/gr_goals-icon2.jpg');
	$('#icon3').attr('src', 'https://www.domain.com/art/gr_goals-icon3.jpg');
	$('#iconstext').html('Long Term<br />Goal (12 months)');
	$('#titletext').html('Long Term Goal (12 months)');
	$('#term').val('12');
	$('.backnext').removeAttr('disabled');
	$('#buttonback').attr('disabled', 'true');
	$('#startdate').val($('#startdate_12').val());
	$('#weight1b').val($('#weight1b_12').val());
	$('#weight1c').val($('#weight1c_12').val());
	$('#weight2a').val($('#weight2a_12').val());
	$('#weight2b').val($('#weight2b_12').val());
	$('#weight2c').val($('#weight2c_12').val());
	$('#weight2d').val($('#weight2d_12').val());
}

Open in new window

0
Comment
Question by:bbdesign
  • 2
4 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36544501
you need to pass the method name which will be called inside saveData() method, after its execution is completed, then that method will be executed
0
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 250 total points
ID: 36544551
something like this

function icon1(){
      savedata("doRest()");
}

function saveData(callbackMethodName)
{
   ....code that is needed to be executed first
  eval(callbackMethodName);
}

function doRest()
{
      $('#icon1').attr('src', 'https://www.domain.com/art/gr_goals-icon1b.jpg');
      $('#icon2').attr('src', 'https://www.domain.com/art/gr_goals-icon2.jpg');
      $('#icon3').attr('src', 'https://www.domain.com/art/gr_goals-icon3.jpg');
      $('#iconstext').html('Long Term<br />Goal (12 months)');
      $('#titletext').html('Long Term Goal (12 months)');
      $('#term').val('12');
      $('.backnext').removeAttr('disabled');
      $('#buttonback').attr('disabled', 'true');
      $('#startdate').val($('#startdate_12').val());
      $('#weight1b').val($('#weight1b_12').val());
      $('#weight1c').val($('#weight1c_12').val());
      $('#weight2a').val($('#weight2a_12').val());
      $('#weight2b').val($('#weight2b_12').val());
      $('#weight2c').val($('#weight2c_12').val());
      $('#weight2d').val($('#weight2d_12').val());
}
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 36544552
Use :

function icon1(){
        $.ajaxSetup({"async":false});
 	savedata();
        $.ajaxSetup({"async":true});
	$('#icon1').attr('src', 'https://www.domain.com/art/gr_goals-icon1b.jpg');
	$('#icon2').attr('src', 'https://www.domain.com/art/gr_goals-icon2.jpg');
	$('#icon3').attr('src', 'https://www.domain.com/art/gr_goals-icon3.jpg');
	$('#iconstext').html('Long Term<br />Goal (12 months)');
	$('#titletext').html('Long Term Goal (12 months)');
	$('#term').val('12');
	$('.backnext').removeAttr('disabled');
	$('#buttonback').attr('disabled', 'true');
	$('#startdate').val($('#startdate_12').val());
	$('#weight1b').val($('#weight1b_12').val());
	$('#weight1c').val($('#weight1c_12').val());
	$('#weight2a').val($('#weight2a_12').val());
	$('#weight2b').val($('#weight2b_12').val());
	$('#weight2c').val($('#weight2c_12').val());
	$('#weight2d').val($('#weight2d_12').val());
}

Open in new window

0
 

Author Comment

by:bbdesign
ID: 36544602
These both look valid, thanks for your help.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…

777 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