使用 jquery ajax 调用时 IE7 内存泄漏

发布于 2025-01-07 05:52:55 字数 862 浏览 0 评论 0原文

我有一个 Web 应用程序,每秒执行一次 ajax 调用来刷新页面的状态 我在 IE7 和 jQuery(1.7.1) ajax 调用中遇到了严重的内存泄漏问题。

为了测试内存泄漏,我创建了一个 html 测试页面,它所做的就是在文档就绪时运行“refreshState”函数。

“refreshState”函数除了进行 ajax 调用和 通过setTimeOut函数设置该函数下次运行的时间。

<script type="text/javascript">
    var url = "http://localhost/QuotesService/QuotesService.svc/GetModel";

    function refreshState() {
        $.ajax({
            url: url,
            cache: false,
            dataType: "json",
            success: function (data) {
                //refresh page data(wasn't activated while testing for memory leaks)
                data = null;
            }
        });

        setTimeout(function () { refreshState() }, 1000);
    }

    $(document).ready(function () {
        refreshState();
    });
</script>

是否有更好的方法来实现此功能以消除内存泄漏?

谢谢 我愿意

I have a web application that does an ajax call every second to refresh the page's state
and I'm experiencing serious memory leak issues with IE7 and jQuery(1.7.1) ajax calls.

To test the memory leak i created a html test page that all it does is run the "refreshState" function on document ready.

The "refreshState" function doesn't do much except from making the ajax call and
setting the next time the function will run through setTimeOut function.

<script type="text/javascript">
    var url = "http://localhost/QuotesService/QuotesService.svc/GetModel";

    function refreshState() {
        $.ajax({
            url: url,
            cache: false,
            dataType: "json",
            success: function (data) {
                //refresh page data(wasn't activated while testing for memory leaks)
                data = null;
            }
        });

        setTimeout(function () { refreshState() }, 1000);
    }

    $(document).ready(function () {
        refreshState();
    });
</script>

Is there a better way to implement this functionality that will eliminate the memory leaks?

Thanks
Ido

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

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

发布评论

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

评论(2

对你的占有欲 2025-01-14 05:52:55

除非绝对有必要,否则它每秒都很尖锐,也许它有助于将 setTimeout 放入 jQuery success 函数中。这样您就可以避免同时进行过多的 Ajax 调用,以防其中一个调用“堵塞”。如果您决定这样做,我还建议添加错误函数,它也会设置超时。这样,错误就不会破坏循环,并且能够“自行重启”。

Unless it's stricly necessary it's sharp on every second, maybe it helps putting the setTimeout inside the jQuery success function. This way you avoid having too many Ajax calls at the same time in case one of them "jams". If you decide to go with this I'd also recommend adding the error-function, which also sets a timeout. That way an error will not break the cycle and it's able to "reboot itself".

孤寂小茶 2025-01-14 05:52:55

我应该想象泄漏是由于 AJAX 调用花费的时间超过 1 秒,因此每个调用都被堆积起来。

您真的需要每秒更新一次吗?尝试使用 5 或 10 秒的间隔,并添加 interval - 1 超时(示例如下)。我敢打赌你的记忆问题就会消失。

另外,我会向您的 AJAX 添加一个 error 处理程序,以便您可以更好地监控 AJAX 调用的任何问题。

function refreshState() {
    $.ajax({
        url: url,
        cache: false,
        dataType: "json",
        timeout: 9000, // 9s timeout
        success: function (data) {
            //refresh page data(wasn't activated while testing for memory leaks)
            data = null;
        },
        error: function(xhr, status, error) {
            console.log(status + " : " + error)
        }
    });

    setTimeout(refreshState, 10000); // 10s interval
}

I should imagine the leak is due to the fact that the AJAX call takes longer than 1s, therefore each call is being stacked up.

Do you REALLY need this to update every second? Try it with a 5 or 10 second interval, and also add an interval - 1 timeout (example below). I'd wager your memory problem goes away.

Also, I'd add an error handler to your AJAX so you can better monitor any issues with the AJAX calls.

function refreshState() {
    $.ajax({
        url: url,
        cache: false,
        dataType: "json",
        timeout: 9000, // 9s timeout
        success: function (data) {
            //refresh page data(wasn't activated while testing for memory leaks)
            data = null;
        },
        error: function(xhr, status, error) {
            console.log(status + " : " + error)
        }
    });

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