如何检查 javascript 函数是否准备好/完成 (jQuery)
我正在开发一个学习计划程序,它从数据库获取数据(语言键、任务、活动等)。因为我需要一个 JSON 字符串,所以我使用 json_encode
对其进行编码,以便在 JavaScript 中使用它。 我有一个不同的函数(用于键、任务、活动等),它获取这些数据并将其写入数组。
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;
}
一切都运转良好。我需要一些帮助,因为我现在必须在 $(document).ready()
中调用此函数。我只想在函数 get_tasks()
完成后(数组充满数据)构建我的学习计划器。否则,我会收到错误。
如何解决这个问题?
这是我在 $(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();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以向函数添加回调:
然后在函数中填充数组后,调用回调函数:
现在您可以使用回调参数来初始化学习计划器:
You can add a callback to the function:
Then after populating the array in the function, call the callback function:
Now you can use the callback parameter to initialise the learning planner:
您应该能够在
$.getJSON
中指定回调,该回调将在请求完成后立即执行。编辑:
您已经这样做了,但为什么不直接调用
$.getJSON
中回调函数末尾的第二个代码块呢?You should be able to specify a callback in
$.getJSON
, which gets executed as soon the request is completed.EDIT:
You're already doing this, but why don't you just call the second code block from the end of the callback funciton in
$.getJSON
?其他答案对我不起作用,因为我有 5 个函数将我的数据与
$.getJSON
一起使用,而且我需要收集所有信息才能启动init_learnplanner()
。经过几个小时的搜索,我发现了 jQuery 函数 ajaxComplete,它对我来说就像一个魅力。 jQuery 跟踪所有已触发的 ajax 调用,并在调用完成时触发分配的任何
.ajaxComplete()
操作。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 startinit_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.我所做的通常是这样的:
简单,看起来像初学者,但它有效:) :D
希望这对你有帮助
What I'm doing is usually something like this:
simple, looks like beginner but it works :) :D
hope this help you