Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
49
rated 0 times [  51] [ 2]  / answers: 1 / hits: 16796  / 14 Years ago, tue, december 7, 2010, 12:00:00

I am working on a learning planner which gets its data (languagekeys, tasks, activities, etc.) from a database. Because I need a JSON string, I encode it with json_encode to work with it in JavaScript.
I have a different function (for keys, tasks, activities, etc.) which gets this data and writes it into an array.



function get_tasks(start_date,end_date){

maxsubtasks=0;
maxtasks=0;

$.getJSON(json_data+?t_startdate=+start_date+&t_enddate=+end_date, function(data) {

tasks=new Array();

$.each(data.tasks, function(i,item){

tasks[i]= new Object();
tasks[i][t_id]=item.t_id;
tasks[i][t_title]=item.t_title;
tasks[i][t_content]=item.t_content;
. . .

if ( i > data.tasks.length) return false;
maxtasks = data.tasks.length;
if(item.t_parent > 0){
maxsubtasks++;
}
});
});
return true;
}


Everything is working just fine. I need some help, because I now have to call this function in $(document).ready(). I want to build my learning planner only once the function get_tasks() is complete (the array is filled with data). Otherwise, I will get errors.



How can this be solved?



Here is what I have in $(document).ready():



if(get_tasks(first_day,last_day) && get_tmp_data()){ // If this function is done
// This function should be fired -- just like a callback in jQuery
init_learnplanner();
}

More From » jquery

 Answers
25

Other answers haven't worked for me because I have 5 functions which use my data with $.getJSON, and I need to have collected all information to even start init_learnplanner().



After several hours of searching, I've discovered the jQuery function ajaxComplete, which works like a charm for me. jQuery tracks all ajax calls that have been fired and triggers anything assigned .ajaxComplete() when one is complete.


[#94698] Sunday, December 5, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deannaalysonl

Total Points: 703
Total Questions: 101
Total Answers: 115

Location: Saint Helena
Member since Tue, Nov 3, 2020
4 Years ago
;