Solved

adding callback to function

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

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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 …
The viewer will learn how to dynamically set the form action using jQuery.
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…

707 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

12 Experts available now in Live!

Get 1:1 Help Now