JavaScript 中途停止执行并评估错误答案?,Console.log 是正确的

标签 javascript jquery

我认为这是不可能的,直到 console.log(); 告诉我发生的事情是不可能的。

我无法理解这是怎么可能的,就像这些变量在代码执行完成之前被修改一样。

我得到了这段 JavaScript 代码,并在其中进行了调试。

它被包裹在这个里面。

$('#buyAmountInput').keyup(function () {
        var buyAmount = parseFloat($(this).val());
        var totalPrice = 0;
        var max = $(this).attr("max");
        var min = $(this).attr("min");

        if (buyAmount != $(this).val()) {
            if (isNaN(buyAmount)) {
                buyAmount = 1;
                $(this).val('');
            } else {
                $(this).val(buyAmount);
            }
        } else {
            if (buyAmount > max) {
                buyAmount = max;
                $(this).val(buyAmount);
            } else if (buyAmount < min) {
                buyAmount = min;
                //$(this).val(buyAmount);
            }
        }
        totalPrice = buyAmount * unitPrice;
        //lots of code trimmed off here.


        largessAmount = Math.round(buyAmount * largessRule.rebate) / 100;
////
        console.log("Buy amount " + buyAmount + " LargessRebate " + largessRule.rebate);
        console.log("Total Price  " + totalPrice);
        console.log("Largess Amount " + largessAmount);
        console.log("New rate " + Number(totalPrice / (buyAmount + largessAmount)).moneyFormat());
        console.log("No .moneyFormat() New rate " + Number(totalPrice / (buyAmount + largessAmount)));
        console.log("( " + totalPrice + " / ( " + buyAmount + " + " + largessAmount + " ))");
////
        $('#unitPrice').html(Number(totalPrice / (buyAmount + largessAmount)).moneyFormat());
});

调试看起来像这样

Buy amount 5000 LargessRebate 20
Total Price  4250
Largess Amount 1000
New rate 0.71
No .moneyFormat() New rate 0.7083333333333334
( 4250 / (5000 + 1000))


function fastKeyListener content_script.js:208

Buy amount 5000 LargessRebate 20
Total Price  4250
Largess Amount 1000
New rate 0.00 //<- What happened here
No .moneyFormat() New rate 0.00008499830003399932 //<- What happened here
( 4250 / (5000 + 1000)) //<- Third line executed this will give good rate..

即使变量是全局的并且此代码位于按键向上 jQuery 回调函数中。 这些变量在 console.log() 执行之前被打印,它们是正确的,但答案是完全错误的。

这是 MoneyFormat(),我认为这不是问题,可以吗?

var effective_bit = -2;

Number.prototype.moneyFormat = function () {
    var num = this;
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * Math.pow(10, -effective_bit) + 0.50000000001);
    cents = num % Math.pow(10, -effective_bit);
    num = Math.floor(num / Math.pow(10, -effective_bit)).toString();
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ', ' + num.substring(num.length - (4 * i + 3));
    if (effective_bit < 0) {
        if (cents < 10 && effective_bit == '-2') cents = "0" + cents;
        money = (((sign) ? '' : '-') + num + '.' + cents);
    } else {
        money = (((sign) ? '' : '-') + num);
    }
    return money;
};

我没有发布完整的代码,因为它非常大,但你可以实时看到它 here

只要输入要购买的单位 4999,然后滚动到 5000 就可以了。尝试输入 5001 或 50000,它会将其重置为 5000,并在此过程中给出错误的答案。

编辑: 可以将问题简化为:如果在执行后仅 1 行使用相同变量生成的相同方程给出了正确答案,即使在计算器上,为什么 console.log() 函数会评估错误答案。

就像这里发生的一些量子一样,请耐心等待,在代码执行期间我无法从一行到另一行做任何事情,没有设置断点,而且这些回调是在它们自己的沙箱中生成的函数,我相信所以它们就像 ajax 线程一样,它们迟早都要完成,它们都是相互独立工作的,所以这里没有任何东西一起工作来搞乱它。你认为这里可能会发生什么?一些暂时的腐败或其他什么?

最佳答案

使用字符串变量进行计算时有时会发生这种情况。 在进行任何计算之前,尝试将 buyAmount 以及来自 HTML 的任何变量转换为数字。

您可以使用Number()函数或parseFloat()

http://jsfiddle.net/rcdmk/63qas2kw/1/

关于JavaScript 中途停止执行并评估错误答案?,Console.log 是正确的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25859124/

相关文章:

javascript - Docusign API - 亲自签名的 View /收件人

javascript - 动态替换 UIWebView 中的 HTML 元素

jquery - 如何使用等列脚本重新计算高度

Jquery按钮不提交请求

Javascript Null 和 Empty 检查

javascript - 如何禁用可折叠面板上的展开折叠提示?

javascript - Javascript/Jquery 的默认日期格式

javascript - Jquery BxSlider 插件帮助创建自定义左右箭头以浏览幻灯片

javascript - 无法删除使用 JavaScript 添加的动态添加行

javascript - 控制 Jquery 路径贝塞尔速度