2

我不知道为什么,但是我的 Javascript 非常慢,需要大约五分钟才能正确完成,有时您会刷新页面并且某些请求尚未处理。

我已经使用了 async:true 希望它会处理得更快一点,但事实并非如此。这是我用来将每个元素保存在#myspace 中的代码。

cn = document.getElementById("myspace").childNodes;
        for (var t = 0; t < cn.length; t++) {
            if (cn[t].nodeType == 1) {
                var n = {
                    id: cn[t].id,
                    left: cn[t].style.left,
                    top: cn[t].style.top
                };
                $.ajax({
                    data: n,
                    url: "/Application/Ajax/__ajaxProfile.php?a=SavePosition",
                    type: "post",
                    cache: true,
                    async: true,
                    success: function (e) {}
                })
            }
        }
        e("Please wait for everything to save, it might take more than a minute.");

        setInterval(function () {

        if ($.active == 0) {

            $('#close-modal').show();

        }

        }, 10);

任何人都知道为什么它的处理如此缓慢并且是一个不错的解决方案?

4

1 回答 1

3
var n_array=new Array();

for (var t = 0; t < cn.length; t++) {
    if (cn[t].nodeType == 1) {
        var n = {
            id: cn[t].id,
            left: cn[t].style.left,
            top: cn[t].style.top
         };
         n_array.push(n);     
     }
}

$.ajax({
   data: n_array,
   url: "/Application/Ajax/__ajaxProfile.php?a=SavePosition",
   type: "post",
   cache: true,
   success: function (e) {}
})

应该更快,并产生更少的流量。ofc 你将不得不稍微改变处理代码......

于 2013-08-26T12:19:24.337 回答