Solved

adding callback to function

Posted on 2011-09-15
4
302 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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)

867 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

18 Experts available now in Live!

Get 1:1 Help Now