递归AJAX帖子,导致计算机运行缓慢

发布于 2024-09-10 03:34:24 字数 865 浏览 5 评论 0原文

我有这个递归循环,在函数内部我至少有 2 个 ajax get/post,并且递归发生在第一个 ajax get 之后。我的函数结构是这样的,

function Loop() {
    $.get(url, data, function(result) {
        for loop to render the result {
            // render the result here
        }
        for loop to get another data using the result {
            $.post(url, result.data, function(postResult) {
                // I don't know what it did here since 
                // I don't have an access to this post
            });
            // is there a way here that i will not proceed if the post is not done yet?
        }
        setTimeout("", 1000); // I wait for 1 second for the post to finish
        Loop(); // call the recursion
    }, "json");
}

谁能告诉我这段代码有什么问题吗?为什么我会收到计算机发出的警告,提示我的脚本导致计算机运行缓慢。我知道这个代码是导致它的原因,但我不知道解决方法。

我知道 get 内的第二个循环会导致大量内存。有没有办法在ajax post未完成的情况下不循环返回?

I have this recursion loop where inside the function I have atleast 2 ajax get/post, and the recursion happens after the first ajax get. my function structure is like this,

function Loop() {
    $.get(url, data, function(result) {
        for loop to render the result {
            // render the result here
        }
        for loop to get another data using the result {
            $.post(url, result.data, function(postResult) {
                // I don't know what it did here since 
                // I don't have an access to this post
            });
            // is there a way here that i will not proceed if the post is not done yet?
        }
        setTimeout("", 1000); // I wait for 1 second for the post to finish
        Loop(); // call the recursion
    }, "json");
}

can anyone tell me what's wrong with this code? why do i get a warning from the computer that my script is causing the computer to run slowly. I know that this code is the one causing it, but I don't know the work around.

I know inside the second loop inside the get is causing a lot of memory. Is there a way that it will not loop back if the ajax post is not finished?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

智商已欠费 2024-09-17 03:34:24

您的 setTimeout 不会整齐地将代码暂停一秒钟:它只会设置一个计时器(在您的情况下为空)事件在特定时间后关闭。脚本的其余部分将继续与此并行执行。

因此,您当前调用递归函数的频率比您想象的要高得多。这是你的第一个问题。

不过,您最大的问题是,无论您在帖子的结果中做什么,这都完全属于另一个范围,并且您无法从那里突破 Loop 函数。您的代码中没有任何内容可以破坏递归,因此它是无限的,并且速度非常快,并且在此基础上发送 Ajax 请求。

您需要更详细地描述您想要实现的目标,也许有人可以告诉您应该如何实现。唯一确定的是你需要使用回调。我写了一个例子,但它做了很多假设。这与我认为您可能想要实现的目标有很多近似,但毫无疑问,您需要对此进行一些调整以满足您的需求。希望它能让您了解需要使用的工作流程:

function Loop() {
    $.get(url, data, function(result) {
        for loop to render the result {
            // render the result here
        }

        // this is what you're looping over in your second loop
        var postQueue = result.someArray;

        renderChildData(postQueue, 0);
    }, "json");
}

function renderChildData(array, index) {

   // this is just one item in the loop
   var currentItem = array[index];

   $.post(url, currentItem, function(postResult) {

       // we have received the result for one item
       // render it, and proceed to fetch the next item in the list

       index++;
       if(index < array.length) {
          renderChildData(array, index);
       }

   });
}

Your setTimeout will not neatly pause the code for one second: it will just set a timer for an (empty, in your case) event to go off after a certain time. The rest of the script will continue to execute parallel to that.

So you're currently calling your recursion function a lot more frequently than you think you are. That's your first problem.

Your biggest problem, though, is that regardless of what you're doign in the result of your post, that's in another scope entirely, and you cannot break out of the Loop function from there. There is nothing in your code to break the recursion, so it is infinite, and very fast, and it sends off Ajax requests on top of that.

You need to describe in more detail what you want to achieve, and perhaps somebody can show you how you should do it. The only thing that is certain is that you need to use callbacks. I've written an example but it's making a lot of assumptions. It's a lot of approximations of what I think you might want to achieve, but no doubt you'll need to tweak this a bit to fit your needs. Hopefully it'll give you an idea of the workflow you need to use:

function Loop() {
    $.get(url, data, function(result) {
        for loop to render the result {
            // render the result here
        }

        // this is what you're looping over in your second loop
        var postQueue = result.someArray;

        renderChildData(postQueue, 0);
    }, "json");
}

function renderChildData(array, index) {

   // this is just one item in the loop
   var currentItem = array[index];

   $.post(url, currentItem, function(postResult) {

       // we have received the result for one item
       // render it, and proceed to fetch the next item in the list

       index++;
       if(index < array.length) {
          renderChildData(array, index);
       }

   });
}
梦毁影碎の 2024-09-17 03:34:24

首先,这一行:

   setTimeout("", 1000); // I wait for 1 second for the post to finish

不会让您的脚本等待,因为它不正确地使用了 setTimeout 函数。我认为您应该考虑使用 setInterval 来代替并执行以下操作:

function Loop() {
    $.get(url, data, function(result) {
        for loop to render the result {
            // render the result here
        }
        for loop to get another data using the result {
            $.post(url, result.data, function(postResult) {
                // I don't know what it did here since 
                // I don't have an access to this post
            });
            // is there a way here that i will not proceed if the post is not done yet?
        }
    }, "json");
}

setInterval( Loop, 1000);

这将使您的函数每 1 秒执行一次。我想这正是您想要获得的。没有理由在这里进行递归调用。

First of all this line:

   setTimeout("", 1000); // I wait for 1 second for the post to finish

doesn't make your script to wait, since it's improper usage of setTimeout function. I think you should consider to use setInterval instead and do it like:

function Loop() {
    $.get(url, data, function(result) {
        for loop to render the result {
            // render the result here
        }
        for loop to get another data using the result {
            $.post(url, result.data, function(postResult) {
                // I don't know what it did here since 
                // I don't have an access to this post
            });
            // is there a way here that i will not proceed if the post is not done yet?
        }
    }, "json");
}

setInterval( Loop, 1000);

This will make execute your function every 1 sec. I guess this is exactly what you wanted to gain. There is no reason to make recursive call here.

橙味迷妹 2024-09-17 03:34:24

当您在页面上使用大量代码时,它基本上会发生..
所以尝试压缩这段代码

it basically happen when you use a huge code on a page ..
so just try to compress this code

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