Solved

How can i execute a javacript after success of another

Posted on 2015-02-19
6
50 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
  • 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 500 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

760 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

23 Experts available now in Live!

Get 1:1 Help Now