淘汰导致“慢速运行脚本” IE中的警告 [英] Knockout causing "slow running script" warning in IE

查看:125
本文介绍了淘汰导致“慢速运行脚本” IE中的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可观察的网格和计算的可观察量。第一行包含用作下面所有行中的值的乘数的百分比率。用户可以编辑百分比率值,Knockout可以处理所有级联更新。我还需要一个单独的文本框,用户可以输入一个新的百分比率,该百分率将应用于网格中的每个百分比。

I have a grid of observables and computed observables. The first row contains percentage rates used as a multiplier against the values in all the rows below. The user can edit the percentage rate value and Knockout handles all the cascading updates. I also need a separate textbox where the user can enter a new percentage rate that will be applied to each of the percentage rates in the grid.

初始绑定工作正常并且更新1个百分比的工作也很好。

Initial binding works fine and updating 1 percentage rate works is fine too.

当用户在文本框中输入一个值并且我遍历视图模型时,会发生javascript警告,将每个百分比率更新为比赛。网格列实际上是每月值,因此更新百分比率的循环仅执行12次。

The javascript warning occurs when the user enters a value in the textbox and I loop through the view model updating each of the percentage rates to match. The grid columns are actually monthly values so the loop to update the percentage rate only executes 12 times.

我尝试了节流扩展器,但它没有解决问题。有什么想法吗?

I tried throttle extender but it didn't solve the problem. Any ideas?

更新不确定它会有所帮助,但我添加了一些代码

Update: not sure it will help, but I added some code

$("#NewRate").change(function (e) {
    var newRate = parseFloat($(this).val());
    for (var i = 0; i < 12; i++) {
        viewModel.resourceCategory.monthAmounts[i].amount(newRate);
    }
});

function ConvertToDate(jsonDateString) {
    var re = /-?\d+/;
    var m = re.exec(jsonDateString);
    return new Date(parseInt(m[0]));
}

function MonthAmount(amount, dateKey) {
    var self = this;
    self.amount = ko.observable(amount).extend({ throttle: 1 }); //using the throttle to avoid "long running script" warning in IE
    self.dateKey = ConvertToDate(dateKey);
    self.monthIndex = self.dateKey.getMonth();
}

function ResourceCategory(name, monthAmounts) {
    var self = this;
    self.name = name;

    self.monthAmounts = ko.utils.arrayMap(monthAmounts, function (monthAmount) {
        return new MonthAmount(monthAmount.Amount, monthAmount.DateKey);
    });

    self.totalAmount = ko.computed(function () {
        var sum = 0;
        for (var i = 0; i < self.monthAmounts.length; i++) {
            sum += parseFloat(self.monthAmounts[i].amount());
        }
        return sum.toFixed(2);
    }).extend({ throttle: 1 }); //using the throttle to avoid "long running script" warning in IE

    self.averageAmount = ko.computed(function () {
        return (self.totalAmount() / self.monthAmounts.length).toFixed(2);
    }).extend({ throttle: 1 }); //using the throttle to avoid "long running script" warning in IE

}
function ResourceCategoriesMonthTotal(monthIndex, resourceCategories) {
    var self = this;
    self.monthIndex = monthIndex;
    self.dateKey = new Date(new Date().getFullYear(), monthIndex, 1);
    self.amount = ko.computed(function () {
        var val = 0;
        for (var i = 0; i < resourceCategories.length; i++) {
            val += parseFloat(resourceCategories[i].monthAmounts[self.monthIndex].amount());
        }
        return (val).toFixed(2);
    }).extend({ throttle: 1 }); //using the throttle to avoid "long running script" warning in IE
}

self.resourceCategoriesMonthTotals = new Array();
for (var monthIndex = 0; monthIndex < 12; monthIndex++) {
    self.resourceCategoriesMonthTotals.push(new ResourceCategoriesMonthTotal(monthIndex, self.resourceCategories));
}

self.resourceCategoriesTotal = ko.computed(function () {
    var val = 0;
    for (var i = 0; i < self.resourceCategoriesMonthTotals.length; i++) {
        val += parseFloat(self.resourceCategoriesMonthTotals[i].amount());
    }
    return (val / self.resourceCategoriesMonthTotals.length).toFixed(2);
}).extend({ throttle: 1 }); //using the throttle to avoid "long running script" warning in IE


推荐答案

好的,这很臭,但我认为问题在于我的页面上有这个:

Ok, this stinks, but I think the problem is that I had this on my page:

<div data-bind="text: ko.toJSON($root)"></div>

删除后,它没有给我慢速运行脚本警告。

After I removed that it didn't give me the "slow running script" warning.

这篇关于淘汰导致“慢速运行脚本” IE中的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆