累计小时的Javascript计数器

时间:2015-02-24 22:40:19

标签: javascript counter

我们需要一个计数器才能显示我们的累积时间,但我似乎无法将其付诸实施。它正在一个旧网站上工作,但我不能让它在新网站上工作。任何人都可以告诉我,如果我错过了下面重要的事情吗?

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 = "&nbsp;Cumulative Fleet Runtime: " + Number(count.toFixed(2)).toLocaleString('en') + " hours (" + Number(fleetyears.toFixed(2)).toLocaleString('en') + " years)&nbsp;"; //simply 'count' if unformatted
 window.setInterval( function(){
  count += INCREMENT; 
  document.getElementById('counter').innerHTML = "&nbsp;Cumulative Fleet Runtime: " + Number(count.toFixed(2)).toLocaleString('en') + " hours (" + Number(fleetyears.toFixed(2)).toLocaleString('en') + " years)&nbsp;"; //simply 'count' if unformatted
 }, msInterval);
});

2 个答案:

答案 0 :(得分:0)

$ 未定义。我认为在运行JS代码之前需要确保包含jQuery。也许你可以更新你的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>

答案 1 :(得分:0)

我得到了它的工作。问题是我试图让它在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 = "&nbsp;Cumulative Fleet Runtime: " + Number(count.toFixed(2)).toLocaleString('en') + " hours (" + Number(fleetyears.toFixed(2)).toLocaleString('en') + " years)&nbsp;"; //simply 'count' if unformatted
 window.setInterval( function(){
  count += INCREMENT; 
  document.getElementById('counter').innerHTML = "&nbsp;Cumulative Fleet Runtime: " + Number(count.toFixed(2)).toLocaleString('en') + " hours (" + Number(fleetyears.toFixed(2)).toLocaleString('en') + " years)&nbsp;"; //simply 'count' if unformatted
 }, msInterval);
});