JS.erb文件中无响应的脚本错误

时间:2015-05-08 16:02:05

标签: javascript ruby-on-rails erb

目前我在ruby on rails项目中的js.erb文件中有下面的代码。发生的事情是,在使用此文件时,页面变得无法响应。有时@updates1可以有超过100条记录。有没有办法在后台处理绘画并保持UI响应。随之而来的是页面变得“锁定/不响应”#34;持续10秒至1分钟。有时我得到这个页面的警告已经没有反应。

我的代码工作需要很长时间才能处理

js.erb中的实际代码

var obj1 = {
    "loads": <%= raw @updates1 %>
};

var count = 0;
var tab_keys = [];

$.each(obj1["loads"], function(k, v){
    $.each(v, function(key, value) {
        count++;
        var addId = $('#' + key).dataTable().fnAddData(value);
        var theNode = $('#' + key).dataTable().fnSettings().aoData[addId[0]];
        theNode.nTr.setAttribute('id','row_' + value[0]);
        $('#' + key).dataTable().fnAdjustColumnSizing();
        tab_keys.push(key);
    });
});



if (count > 0) {
    var message = count + " More Loads!";

    $.titleAlert(message, {
        requireBlur:false,
        stopOnFocus:false,
        duration:8000,
        interval:700
    });

    tab_keys = $.unique(tab_keys);
    $.each(tab_keys, function(k, v){
        var tab_key = v.replace(new RegExp("_load_results", "gi"), "");
        var tab = 'a[href$="#tab_' + tab_key + '"]';
        if ($(tab).parent().hasClass("ui-state-active") == true)
            $(tab).css("font-weight","bold").parent().effect( 'highlight', {color: 'Blue'}, 7000);
        else
            $(tab).css("font-weight","bold").parent().css("background","#00D9D9").effect( 'highlight', {color: 'Blue'}, 7000);
    });

    if ( $('#prefs_can_be_screen_notified').val() == "1") {
        playSound('/assets/'+$('#prefs_default_sound').val());
    }
}

它正在做什么的结果:顺便说一句,我把obj1的长度缩短了,以便在这里张贴......有时可能有100多个帖子......

var obj1 = {
    "loads": [{'25455_1431099504_load_results': ['13345499','2015-05-08 15:48:37 UTC','<abbr class=\"timeago\" title=\"2015-05-08 10:48:37\">1 minute</abbr>','Albuquerque, NM','Colorado Springs, CO','05/12','05/13','Full','Flatbed','','53']}, {'25455_1431099504_load_results': ['13345497','2015-05-08 15:48:37 UTC','<abbr class=\"timeago\" title=\"2015-05-08 10:48:37\">1 minute</abbr>','Gypsum, CO','Lincoln, NE','05/08','05/11','Full','Flatbed','','53']}]
};

var count = 0;
var tab_keys = [];

$.each(obj1["loads"], function(k, v){
    $.each(v, function(key, value) {
        count++;
        var addId = $('#' + key).dataTable().fnAddData(value);
        var theNode = $('#' + key).dataTable().fnSettings().aoData[addId[0]];
        theNode.nTr.setAttribute('id','row_' + value[0]);
        $('#' + key).dataTable().fnAdjustColumnSizing();
        tab_keys.push(key);
    });
});



if (count > 0) {
    var message = count + " More Loads!";

    $.titleAlert(message, {
        requireBlur:false,
        stopOnFocus:false,
        duration:8000,
        interval:700
    });

    tab_keys = $.unique(tab_keys);
    $.each(tab_keys, function(k, v){
        var tab_key = v.replace(new RegExp("_load_results", "gi"), "");
        var tab = 'a[href$="#tab_' + tab_key + '"]';
        if ($(tab).parent().hasClass("ui-state-active") == true)
            $(tab).css("font-weight","bold").parent().effect( 'highlight', {color: 'Blue'}, 7000);
        else
            $(tab).css("font-weight","bold").parent().css("background","#00D9D9").effect( 'highlight', {color: 'Blue'}, 7000);
    });

    if ( $('#prefs_can_be_screen_notified').val() == "1") {
        playSound('/assets/'+$('#prefs_default_sound').val());
    }
}

以上是从这个AJax脚本运行的:

    if (load_search_count > 0) {
        $.ajax({
            url: "/classics/0/refresh_load_tables",
            type: "POST",
            success: function (data) {
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert('error: ' + textStatus + ': ' + errorThrown);
            }
        });
        return false;
    }

0 个答案:

没有答案