调试计数器代码

时间:2016-06-10 23:35:50

标签: javascript wordpress counter

我有一个网站,我作为网页设计师继承,其上有一个不起作用的计数器。该网站在Wordpress 4.5.2上运行,计数器由三部分组成。它应显示在此页面https://electratherm.com/上方累积车队运行时间。

第一部分只是此页面上的div标记:

<div id="counter" style="text-align: center;"></div>

第二部分由客户编辑,它显示在源代码的底部:

<script>
//update next three variables once per week using current fleet data

var START_DATE = new Date("June 5, 2016 23:59:00"); 

// the date and time when run hours were known

var START_VALUE = 548047; 

// the run hours known at the date above

var INCREMENT = 0.0041; 

// 4000 fleet hrs per week = .0050 per sec, how many fleet run hours are added every second of the day. # of week hours / 7/24/60/60
</script>

第三部分是源代码中较低的实际javascript文件:

<script src="https://electratherm-electratherm.netdna-ssl.com/wp-content/themes/Impreza-child/js/counter.js" type="text/javascript">

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

This was working not to long ago but it stopped now. Not sure what is going on but I don't see any errors in the Debug console in Firefox.

2 个答案:

答案 0 :(得分:0)

您的网页上出现了jQuery错误。

  

未捕获错误:语法错误,无法识别的表达式:.w-nav-item

我猜你最近在改变导航?如果是这种情况,那么你在那里做的事情会导致jQuery中断,并且一旦抛出JavaScript错误,其余部分就会非常不可靠。我仔细阅读了您发布的代码,并没有注意到任何明显的错误。

至于修正错误,请查看导航中的href,确保您没有使用任何特殊字符。

答案 1 :(得分:0)

jQuery永远不会调用ready函数。

您在此行的plugins.js文件中似乎存在问题:

jQuery('.w-nav-item a[href*=#]').parents('.w-nav-item').removeClass('current-menu-item');

我认为这是因为#没有像这样正确转义:

jQuery('.w-nav-item a[href*=\\#]').parents('.w-nav-item').removeClass('current-menu-item');

这可能是阻止jQuery执行它的就绪功能的原因,但在它修复之前我无法分辨。