[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

adding callback to function

Posted on 2011-09-15
4
Medium Priority
?
312 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:Gurvinder Pal Singh
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:Gurvinder Pal Singh
Gurvinder Pal Singh earned 1000 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 83

Accepted Solution

by:
leakim971 earned 1000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses
Course of the Month18 days, 21 hours left to enroll

834 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