为什么我的JS不工作(在wordpress中)?

时间:2013-05-09 15:37:42

标签: javascript

我要做的是在我的页面上有一个数字计数,它以稳定的增量/间隔增加。我创建了风格化的图像来表示计数中的每个数字0-9,所以我还需要用它们各自的图像替换每个数字。代码中的第一个函数表示该任务,第二个脚本是实际的计数。我正在使用wordpress,所以我添加了JS文件并将其排入header.php文件中。

这就是我遇到问题的地方:我能够让计数器工作,但counterimages(input)功能不想为我工作。这可能是我试图“调用”WordPress页面上的函数的一个问题。

如果有人能帮助我,我将非常感激!

function counterimages(input) {
var output = ""

for (var i = 0; i < input.length; i++) {
    var chr = input.substring(i, i + 1)
    if (chr == '£') {
        output += '<img border="0" src="img/pound.gif">';
    } else if (chr == '.') {
        output += '<img border="0" src="img/dot.gif">';
    } else {
        output += '<img border="0" src="http://eatiply3.staging.wpengine.com/wp-content/uploads/2013/05/'+(chr+1)+'.png">';
    }
return output;
}

var START_DATE = new Date("October 21, 2012 22:30:00"); // put in the starting date here
var INTERVAL = 1; // refresh interval in seconds
var INCREMENT = 769.2;  // increase per tick (1/0.0013 ~ 769)
var START_VALUE = 35000; // initial value when it's the start date
var count = 0;

jQuery(document).ready(function($) {
 var msInterval = INTERVAL * 1000;
 var now = new Date();
 count = parseInt((now - START_DATE)/msInterval) * INCREMENT + START_VALUE;
 $('#counter').html(count.toFixed(0));

 window.setInterval( function(){
    count += INCREMENT; 
    $('#counter').html(count.toFixed(0));
 }, msInterval);

});`

1 个答案:

答案 0 :(得分:1)

您在counterimages功能

中缺少括号

http://jsfiddle.net/Rhpjw/

function counterimages(input) {
    var output = ""

    for (var i = 0; i < input.length; i++) {
        var chr = input.substring(i, i + 1)
        if (chr == '£') {
            output += '<img border="0" src="img/pound.gif">';
        } else if (chr == '.') {
            output += '<img border="0" src="img/dot.gif">';
        } else {
            output += '<img border="0" src="http://eatiply3.staging.wpengine.com/wp-content/uploads/2013/05/' + (chr + 1) + '.png">';
        }
        return output;
    }
}

var START_DATE = new Date("October 21, 2012 22:30:00"); // put in the starting date here
var INTERVAL = 1; // refresh interval in seconds
var INCREMENT = 769.2; // increase per tick (1/0.0013 ~ 769)
var START_VALUE = 35000; // initial value when it's the start date
var count = 0;

jQuery(document).ready(function ($) {
    var msInterval = INTERVAL * 1000;
    var now = new Date();
    count = parseInt((now - START_DATE) / msInterval) * INCREMENT + START_VALUE;
    $('#counter').html(count.toFixed(0));

    window.setInterval(function () {
        count += INCREMENT;
        $('#counter').html(count.toFixed(0));
    }, msInterval);
});