-1

我正在尝试做的是在我的页面上以稳定的增量/间隔增加一个数字计数。我创建了程式化的图像来表示计数中的每个数字 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);

});`
4

1 回答 1

0

您在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);
});
于 2013-05-09T15:49:47.980 回答