Solved

adding callback to function

Posted on 2011-09-15
4
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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)

696 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