Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 64
  • Last Modified:

How can i execute a javacript after success of another

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
jaylab2
Asked:
jaylab2
  • 3
  • 2
1 Solution
 
OriNetworksCommented:
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
 
jaylab2Author Commented:
So what's callback function is used for ? i am new into this !
0
 
OriNetworksCommented:
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
 
jaylab2Author Commented:
Thank you for your reply but can you give example on my script so it will help understand more.
0
 
OriNetworksCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now