javascript 如何检查javascript函数是否准备好/完成(jQuery)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4374950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if javascript function is ready/done (jQuery)
提问by Chris
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_encodeto 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.
我正在开发一个学习规划器,它从数据库中获取数据(语言键、任务、活动等)。因为我需要一个 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;
}
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.
一切正常。我需要一些帮助,因为我现在必须在$(document).ready(). 我只想在函数get_tasks()完成后构建我的学习计划器(数组填充了数据)。否则,我会得到错误。
How can this be solved?
如何解决这个问题?
Here is what I have in $(document).ready():
这是我所拥有的$(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();
}
回答by Guffa
You can add a callback to the function:
您可以向函数添加回调:
function get_tasks(start_date, end_date, callback) {
Then after populating the array in the function, call the callback function:
然后在函数中填充数组后,调用回调函数:
if (callback) callback();
Now you can use the callback parameter to initialise the learning planner:
现在您可以使用回调参数来初始化学习计划器:
get_tasks(first_day, last_day, function() {
init_learnplanner();
});
回答by Chris
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().
其他答案对我不起作用,因为我有 5 个函数使用我的数据$.getJSON,我需要收集所有信息才能开始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.
经过几个小时的搜索,我发现了 jQuery 函数ajaxComplete,它对我来说就像一个魅力。jQuery 跟踪所有已触发的 ajax 调用,并在.ajaxComplete()完成时触发任何分配的调用。
回答by joni
You should be able to specify a callback in $.getJSON, which gets executed as soon the request is completed.
您应该能够在 中指定一个回调$.getJSON,它会在请求完成后立即执行。
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?
编辑:您已经在这样做了,但是为什么不从回调函数的末尾调用第二个代码块$.getJSON呢?
回答by AnD
What I'm doing is usually something like this: simple, looks like beginner but it works :) :D
我正在做的事情通常是这样的:简单,看起来像初学者,但它有效:) :D
<script type="text/javascript">
var isBusy = true;
$(document).ready(function () {
// do your stuff here
isBusy = false;
});
function exampleajax() {
if(isBusy) return false;
isBusy=true;
$.ajax({
async: true,
type: 'POST',
url: "???.asp",
dataType: "jsonp",
data: qs,
error: function(xhr, ajaxOptions, thrownError){
//console.log(xhr.responseText + " AJAX - error() " + xhr.statusText + " - " + thrownError);
},
beforeSend: function(){
//console.log( "AJAX - beforeSend()" );
},
complete: function(){
//console.log( "AJAX - complete()" );
isBusy = false;
},
success: function(json){
//console.log("json");
}
});
}
</script>
hope this help you
希望这对你有帮助

