如何检查 javascript 函数是否准备好/完成 (jQuery)

发布于 2024-10-06 13:12:23 字数 1278 浏览 6 评论 0原文

我正在开发一个学习计划程序,它从数据库获取数据(语言键、任务、活动等)。因为我需要一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

椒妓 2024-10-13 13:12:23

您可以向函数添加回调:

function get_tasks(start_date, end_date, callback) {

然后在函数中填充数组后,调用回调函数:

if (callback) callback();

现在您可以使用回调参数来初始化学习计划器:

get_tasks(first_day, last_day, function() {
    init_learnplanner();
});

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();
});
你与清晨阳光 2024-10-13 13:12:23

您应该能够在 $.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?

摇划花蜜的午后 2024-10-13 13:12:23

其他答案对我不起作用,因为我有 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 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.

遇见了你 2024-10-13 13:12:23

我所做的通常是这样的:
简单,看起来像初学者,但它有效:) :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>

希望这对你有帮助

What I'm doing is usually something like this:
simple, looks like beginner but it works :) :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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文