如何在更改后继续添加值,如果控制 javascript

How to continue adding values after changing if stetment javascript

本文关键字:如果 控制 javascript 添加 继续      更新时间:2023-09-26

我制作了一个计时器,计算花费的时间乘以价格,现在我们必须制作一个"欢乐时光",在某些时间每秒价格发生变化,现在的问题是当价格改变时,价格和总金额会自动更改新值,以强制 JavaScript 继续添加新价格而不更改新金额只是继续添加。 谢谢你的 sry 我的英语不好。

function timedCount() { 
var date = new Date();
if (date.getHours() > 3 && date.getHours() < 8)
{
    PricePeerSec = 100;
} else {
    PricePeerSec = 333;
} 
    document.getElementById('myForm').style.background="#FFFFCC";
    document.getElementById('txt').value = seconds;
    document.getElementById('din').value = totalPrice;
    Seconds = Seconds + 1;
    totalPrice = Seconds * PricePeerSecond;
    timer = setTimeout("timedCount()", 1000);
}

秒数所在的输入框的 ID 为"txt"总去的输入框有id"din"

如果我正确理解了你的问题,你需要这样的东西:

var secs = 0,
    totalPrice = 0,
    timer;
function timedCount() {
    var hour = new Date().getHours(),
        price = (hour > 3 && hour < 8) ? 111 : 333;
    secs += 1;
    totalPrice += price;
    document.getElementById('myForm').style.background="#FFFFCC";
    document.getElementById('txt').value = secs;
    document.getElementById('din').value = totalPrice;
    timer = setTimeout(timedCount, 1000);
}

jsFiddle的现场演示。

相关文章: