Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can i execute a javacript after success of another

Posted on 2015-02-19
6
Medium Priority
?
60 Views
Last Modified: 2015-02-20
Hello i do have  these functions

function egnyt_facebook(pixel,val, cny) {
  var cd = {};
  cd.value = val;
  cd.currency = cny;
  _fbq.push(['track',pixel, cd]);
}
//combine facebook and google
function egnyt_track(pixel, val, cny,type ,process,name){

	egnyt_facebook(pixel, val , cny);
	_gaq.push(['_trackEvent',type, process, name]);
       window.location='';


}

Open in new window


I do need to call this function egnyt_track(pixel, val, cny,type ,process,name) on onclick event and need to be sure that _gaq.push(['_trackEvent',type, process, name]); is executed after egnyt_facebook(pixel, val , cny); this and after all these executed i do need to redirect to certain page and if i need to know if i can delay a normal <a href=""> combined with onlclick event not to redirect before executing the function anyone can help me sorting this out with an example based on the script above.
0
Comment
Question by:jaylab2
[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
  • 3
  • 2
6 Comments
 
LVL 17

Expert Comment

by:OriNetworks
ID: 40618955
To my knowledge, javascript will only reach the next line if the previous line completed so _gaq.push will only run after egnyt_facebook. As for changing a <a href=""> to delay the navigation I don't think that's possible by default behavior. It may work if you are able to use
<a href="#" onclick="completeBeforeRedirect('http://google.com')">test</a> 

Open in new window

you could then have a javascript function to run code before changing window.location
function completeBeforeRedirect(myUrl) {
//do stuff
//now go to url
window.location = myUrl;
}

Open in new window

0
 

Author Comment

by:jaylab2
ID: 40618965
So what's callback function is used for ? i am new into this !
0
 
LVL 17

Expert Comment

by:OriNetworks
ID: 40619006
I think I should first clarify that there are situations where javascript would not be synchronous and I only know of that to be the case with ajax, otherwise javascript runs line by line and only after the previous line completes. a callback function is a technique and the same thing can be accomplished without using a callback.
for example with a callback you could do
function doMoreStuff(myString) {
  alert(myString)
}
function processMyData(func) {
//do something
   func('this is a test');
}
//i could pass a function to a function
processMyData(doMoreStuff);

Open in new window


Without callbacks I could do
function doMoreStuff(myString) {
  alert(myString)
}
function processMyData(func) {
//do something
   
return true;
}



if (processMyData()===true)
{
doMoreStuff('success');
//do other stuff, call other functions

}
else
{
alert('fail');
}

Open in new window

0
 

Author Comment

by:jaylab2
ID: 40619017
Thank you for your reply but can you give example on my script so it will help understand more.
0
 
LVL 17

Accepted Solution

by:
OriNetworks earned 2000 total points
ID: 40619494
Again there are other ways to do this, but it does allow for reusable functions. the key here is to keep the purpose of their functions simple.

function egnyt_facebook(pixel,val, cny, callbackWhenComplete,type,process,name) {
  var cd = {};
  cd.value = val;
  cd.currency = cny;
  _fbq.push(['track',pixel, cd]);
callbackWhenComplete(type ,process,name);
}
//combine facebook and google
function egnyt_track(type ,process,name){
        //this call to the egnyt_facebook function is no longer needed because we already finished before we got to this function
	//egnyt_facebook(pixel, val , cny);
	_gaq.push(['_trackEvent',type, process, name]);
       window.location='';


}


//you call the one function, in the 3rd parameter we give it the callback function and also specify additional parameters to pass to the callback.
egnyt_facebook(pixel, val , cny, egnyt_track, type, process, name);

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

718 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