Javascript 累计小时数计数器
Javascript Counter for Cumulative Hours
我们需要一个计数器来显示我们的累计小时数,但我似乎无法让它运行。它在一个旧网站上工作,但我无法让它在新网站上工作。谁能告诉我是否遗漏了以下重要内容?
HTML(缩写):
<html lang="en-US">
<head></head>
<body>
<div id="counter"></div>
<script src="../js/counter.js" type="text/javascript">
</body>
</html>
Javascript 文件 (counter.js):
//update next three variables once per week using current fleet data
var START_DATE = new Date("February 17, 2015 23:59:00"); // the date and time when run hours were known
var START_VALUE = 316398; // the run hours known at the date above
var INCREMENT = 0.0055; // 4000 fleet hrs per week = .0066 per sec, how many fleet run hours are added every second of the day
var INTERVAL = 1; // in seconds
var count = 0;
var fleetyears = 0;
$(document).ready(function() {
var msInterval = INTERVAL * 1000;
var now = new Date();
count = parseInt((now - START_DATE)/msInterval) * INCREMENT + START_VALUE;
fleetyears = count / 8766;
document.getElementById('counter').innerHTML = " Cumulative Fleet Runtime: " + Number(count.toFixed(2)).toLocaleString('en') + " hours (" + Number(fleetyears.toFixed(2)).toLocaleString('en') + " years) "; //simply 'count' if unformatted
window.setInterval( function(){
count += INCREMENT;
document.getElementById('counter').innerHTML = " Cumulative Fleet Runtime: " + Number(count.toFixed(2)).toLocaleString('en') + " hours (" + Number(fleetyears.toFixed(2)).toLocaleString('en') + " years) "; //simply 'count' if unformatted
}, msInterval);
});
$
未定义。我认为您需要确保 jQuery 包含在 运行 您的 JS 代码之前。也许你可以更新你的 HTML 像:
<html lang="en-US">
<head></head>
<body>
<div id="counter"></div>
<script src="https://code.jquery.com/jquery-1.11.2.min.js" type="text/javascript"></script>
<script src="../js/counter.js" type="text/javascript">
</body>
</html>
我让它工作了。问题是我试图让它在 Wordpress 中工作。我不能在 wordpress 中使用 $ 所以我不得不用 jQuery:
替换它
//update next three variables once per week using current fleet data
var START_DATE = new Date("February 17, 2015 23:59:00"); // the date and time when run hours were known
var START_VALUE = 316398; // the run hours known at the date above
var INCREMENT = 0.0055; // 4000 fleet hrs per week = .0066 per sec, how many fleet run hours are added every second of the day
var INTERVAL = 1; // in seconds
var count = 0;
var fleetyears = 0;
jQuery(document).ready(function() {
var msInterval = INTERVAL * 1000;
var now = new Date();
count = parseInt((now - START_DATE)/msInterval) * INCREMENT + START_VALUE;
fleetyears = count / 8766;
document.getElementById('counter').innerHTML = " Cumulative Fleet Runtime: " + Number(count.toFixed(2)).toLocaleString('en') + " hours (" + Number(fleetyears.toFixed(2)).toLocaleString('en') + " years) "; //simply 'count' if unformatted
window.setInterval( function(){
count += INCREMENT;
document.getElementById('counter').innerHTML = " Cumulative Fleet Runtime: " + Number(count.toFixed(2)).toLocaleString('en') + " hours (" + Number(fleetyears.toFixed(2)).toLocaleString('en') + " years) "; //simply 'count' if unformatted
}, msInterval);
});
我们需要一个计数器来显示我们的累计小时数,但我似乎无法让它运行。它在一个旧网站上工作,但我无法让它在新网站上工作。谁能告诉我是否遗漏了以下重要内容?
HTML(缩写):
<html lang="en-US">
<head></head>
<body>
<div id="counter"></div>
<script src="../js/counter.js" type="text/javascript">
</body>
</html>
Javascript 文件 (counter.js):
//update next three variables once per week using current fleet data
var START_DATE = new Date("February 17, 2015 23:59:00"); // the date and time when run hours were known
var START_VALUE = 316398; // the run hours known at the date above
var INCREMENT = 0.0055; // 4000 fleet hrs per week = .0066 per sec, how many fleet run hours are added every second of the day
var INTERVAL = 1; // in seconds
var count = 0;
var fleetyears = 0;
$(document).ready(function() {
var msInterval = INTERVAL * 1000;
var now = new Date();
count = parseInt((now - START_DATE)/msInterval) * INCREMENT + START_VALUE;
fleetyears = count / 8766;
document.getElementById('counter').innerHTML = " Cumulative Fleet Runtime: " + Number(count.toFixed(2)).toLocaleString('en') + " hours (" + Number(fleetyears.toFixed(2)).toLocaleString('en') + " years) "; //simply 'count' if unformatted
window.setInterval( function(){
count += INCREMENT;
document.getElementById('counter').innerHTML = " Cumulative Fleet Runtime: " + Number(count.toFixed(2)).toLocaleString('en') + " hours (" + Number(fleetyears.toFixed(2)).toLocaleString('en') + " years) "; //simply 'count' if unformatted
}, msInterval);
});
$
未定义。我认为您需要确保 jQuery 包含在 运行 您的 JS 代码之前。也许你可以更新你的 HTML 像:
<html lang="en-US">
<head></head>
<body>
<div id="counter"></div>
<script src="https://code.jquery.com/jquery-1.11.2.min.js" type="text/javascript"></script>
<script src="../js/counter.js" type="text/javascript">
</body>
</html>
我让它工作了。问题是我试图让它在 Wordpress 中工作。我不能在 wordpress 中使用 $ 所以我不得不用 jQuery:
替换它//update next three variables once per week using current fleet data
var START_DATE = new Date("February 17, 2015 23:59:00"); // the date and time when run hours were known
var START_VALUE = 316398; // the run hours known at the date above
var INCREMENT = 0.0055; // 4000 fleet hrs per week = .0066 per sec, how many fleet run hours are added every second of the day
var INTERVAL = 1; // in seconds
var count = 0;
var fleetyears = 0;
jQuery(document).ready(function() {
var msInterval = INTERVAL * 1000;
var now = new Date();
count = parseInt((now - START_DATE)/msInterval) * INCREMENT + START_VALUE;
fleetyears = count / 8766;
document.getElementById('counter').innerHTML = " Cumulative Fleet Runtime: " + Number(count.toFixed(2)).toLocaleString('en') + " hours (" + Number(fleetyears.toFixed(2)).toLocaleString('en') + " years) "; //simply 'count' if unformatted
window.setInterval( function(){
count += INCREMENT;
document.getElementById('counter').innerHTML = " Cumulative Fleet Runtime: " + Number(count.toFixed(2)).toLocaleString('en') + " hours (" + Number(fleetyears.toFixed(2)).toLocaleString('en') + " years) "; //simply 'count' if unformatted
}, msInterval);
});