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

JavaScript stops execution half way and evaluates wrong answer?, Console.log is correct

本文关键字:Console log 答案 错误 执行 评估 JavaScript      更新时间:2023-09-26

直到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;
};

我没有发布整个代码,因为它很大,但你可以在这里看到它的实时

只需放入4999的单位即可购买,然后滚动到5000即可。。试着输入5001或50000,它会将其重置为5000,并在这个过程中给出错误的答案。

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

就像这里发生的一些量子一样,请记住,在代码执行过程中,从一行到另一行我什么都做不了,没有设置断点,也没有设置任何断点,加上这些回调是在自己的沙箱中生成的函数,我相信它们就像ajax线程一样,迟早都会完成,它们都是独立工作的,所以这里没有什么能把事情搞砸的。你认为这里可能发生什么?一些暂时的腐败还是什么?

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

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

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