gpt4 book ai didi

javascript - Jscript 在开发和测试机器上返回不同的(计算的)结果 - 使用相同的代码

转载 作者:行者123 更新时间:2023-11-28 01:14:37 24 4
gpt4 key购买 nike

好的,在我的开发机器上,我有以下代码:

JScript

setInterval(function () { myTimer(); }, 1000);

function myTimer() {
var d = new Date();
var hrs = d.getHours();
var min = d.getMinutes();
var sec = d.getSeconds();
var dayOfWeek = d.getDay();

// Let's do this logically.
// To get the number of victims so far today we need to know how many seconds have passed so far today.
var totalSeconds = sec + (min * 60) + (hrs * 3600); // Number of Seconds passed so far today.
// Now multiply that by 14 in accordance with Symantec's research.
var VictimsToday = totalSeconds * 14;

// To get the number of victims this week we need to multiply
// The number of seconds in a day by the number of full days and add the number of seconds of this partial day.
var TotalSecondsInADay = 86400; // According to Google.
var DaysSoFarThisWeek = dayOfWeek + 1; // Removes today as a partial day.
// We already know how many seconds are in this partial day = totalSeconds
// So our calculation is
var tsD = TotalSecondsInADay * DaysSoFarThisWeek;
var VictimsThisWeek = (totalSeconds + tsD) * 14;

// Now we get the Day of the Year remove today as a partial day and calculate
// Number of seconds a day by the number of complete days so far + todays partial day
var DOY = DayOfYear();
var tsY = DOY * totalSeconds;
var VictimsThisYear = (totalSeconds + tsY) * 14;

document.getElementById("FooterStatistics").innerHTML = Intl.NumberFormat().format(VictimsToday);
document.getElementById("FooterStatistics2").innerHTML = Intl.NumberFormat().format(VictimsThisWeek);
document.getElementById("FooterStatistics4").innerHTML = Intl.NumberFormat().format(VictimsThisYear);
}

function DayOfYear() {
var today = new Date();
var first = new Date(today.getFullYear(), 0, 1);
var theDay = Math.round(((today - first) / 1000 / 60 / 60 / 24) + 0.5, 0);
return theDay;
}

HTML 为:

body

<div id="FooterStatistics"></div>
<div id="FooterStatistics2"></div>
<div id="FooterStatistics4"></div>

现在在开发机器上,DIV 中返回的计算结果是:

Results

但是,当我在 W3Schools Tryit 编辑器上运行此命令时:http://www.w3schools.com/js/tryit.asp?filename=tryjs_date_getday

我得到了完全不同的计算结果:

TryIt results

现在我知道由于时区差异,数字会有所不同 - 实际计算不应有所不同。 Dev 机器的第二个结果比第一个结果小,这是不应该的,而第三个结果显然甚至不是一个数字 - TryIt 编辑器中的结果提供了更合理的答案。

怎么会这样?

最佳答案

创建应用程序时,继续使用 console.log() 检查值。这确实有助于了解 future 正在发生的事情。虽然这不能替代调试,但它会有所帮助。只有当您的代码即将上线,或者您确定它可以工作并且抽象得很好时,您才能删除它们。

PS:避免使用 w3schools.com。使用MDN 。好多了。

关于javascript - Jscript 在开发和测试机器上返回不同的(计算的)结果 - 使用相同的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23994604/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com