PDA

View Full Version : run function after other functions


Adam_C
03-27-2011, 04:57 PM
Hi, i am creating a web application, and in it I use two functions do determine whether or not a user is logged in to flickr and/or facebook, then depending on where they are logged in, it loads the respective photos.

my only problem is how do i make sure the 'loadPhotos()' function runs only after I have filled arrays with photo information (i.e. name, link etc).

for example,


function getFbPhotos() {
//use fb api to fill arrays
names.push(name);
links.push(link);
}

function getFlPhotos() {
//use flickr api to fill arrays
names.push(name);
links.push(link);
}

function getAllPhotos() {
//contact fb and fl arrays
}

function loadFbPhotos() {
//fill divs with array info
}

function loadFlPhotos() {
//fill divs with array info
}

function loadAllPhotos() {
//fill divs with array info
}

function loadPhotos() {
if (loggedInToFb() == true && loggedInToFl() == false) {
loadFbPhotos();
} else if (loggedInToFb() == false && loggedInToFl() == true) {
loadFlPhotos();
} else if (loggedInToFb() == true && loggedInToFl() == true) {
loadAllPhotos();
} else {
//cant do anything
}
}

function mainFunction(){
getFbPhotos();
getFlPhotos();
getAllPhotos();
loadAllPhotos(); //HERE IS THE PROBLEM
}


I hope this is easy enough to understand...

p.s my code is a bit more complex than this and uses object and other functions, but if you could use the functions above as a 'simple' way of solving this problem, then i could implement it into my code.

thanks for reading :D

ranjan
03-28-2011, 02:14 AM
http://ajaxian.com/archives/polling-for-loaded-content-instead-of-simple-settimeout

dahliacreative
03-28-2011, 01:19 PM
Just fire the function from within the first function for example:

function loadPhotos () {
}

function dothisfirst () {
do lots of stuff in here.
dothissecond();
}

function dothissecond () {
do lots of stuff in here.
loadPhotos();
}

Adam_C
03-28-2011, 08:11 PM
http://ajaxian.com/archives/polling-for-loaded-content-instead-of-simple-settimeout

Thanks, I'm pretty sure this is what I'm looking for however, I don't really know how I would implement this into my code, namely the arguments part. I have got the function in the right place, however my function which gets the photo information fills an array. I have tried removing the 'window' section in the function 'executeWhenLoaded' because the variable I am waiting to load is in an object (more specifically 'myObject.photoFeed.Facebook.thumbNames').

How would i put that into the arguments?

ranjan
03-29-2011, 04:51 AM
Easiest ways is to create global variable flags like

var isAllPhotosLoaded = false;


Once the function loadAllPhotos executes set this flag to true.

And poll for true values, use the simpler setTimeOut method


function startPolling() {
setTimeout('pollForEvents()', 100);
}
function pollForEvents() {
if (isAllPhotosLoaded === true && isAnyOtherCheck === true) {

//do something with the event

} else {
//poll again soon
startPolling();
}
}


Once this works read about arguments http://www.seifi.org/javascript/javascript-arguments.html
https://developer.mozilla.org/en/JavaScript/Reference/functions_and_function_scope/arguments

Then go visit the previous link

Adam_C
03-29-2011, 01:52 PM
That works perfectly ranjan! thank you so much :) Seems there is still a lot more to learn in JS...