停止并重置Harvest's Tick Counter jQuery插件

时间:2015-03-28 16:56:29

标签: javascript jquery jquery-plugins harvest

任何人都知道如何停止并重置Harvest's Tick计数器jQuery插件?我想停止特定号码的计数器并重置为主要启动号码。

您可以查看我的代码here

HTML标记:

<span class="tick tick-flip tick-promo">5,000</span>

jQuery Logic:

<script type="text/javascript">
$(document).ready(function () {
    startCounter();
});

function startCounter() {
    $('.tick').ticker({
        delay: 1000,
        incremental: 1,
        separators: true
    });
}

var myCounter = setInterval(function resetCounter() {
    var lsCurrentTick = $('.tick').find('.tick-new').text();

    if (lsCurrentTick > 5010) {
        $.fn.ticker.stop();
    }
}, 1000);

</script>

2 个答案:

答案 0 :(得分:1)

我不得不read the code来解决这个问题。这是DEMO

$(startCounter);

function startCounter() {
    var tickObj = $('.tick').ticker({
        delay: 1000,
        incremental: 1,
        separators: true
    })[0];
    setInterval(function () {
        if (tickObj.value >= 5002) {
            tickObj.stop();
            tickObj.value = 5000;
            tickObj.start();
        }
    }, 1000);
}

如果你感到勇敢,可以惹恼Tick.prototype.tick DEMO

function startCounter() {
    var tickObj = $('.tick').ticker({
        delay: 1000,
        incremental: 1,
        separators: true
    })[0];
    tickObj.tick = (function (tick) {
        return function () {
            var ret = tick.call(tickObj);
            if (tickObj.value > 5002) {
                tickObj.stop();
                tickObj.value = 5000;
                tickObj.start();
            }
            return ret;
        };
    }(tickObj.tick));
}

答案 1 :(得分:1)

引用自动收报机并重置您必须执行的操作

ticker[0].stop();
ticker[0].value = 5000;
ticker[0].start();

完整示例

$(document).ready(function () {
    var ticker;
    startCounter();
});

function startCounter() {
    ticker = $('.tick').ticker({
        delay: 1000,
        incremental: 1,
        separators: true,
    });
}
var myCounter = setInterval(function resetCounter() {
    var lsCurrentTick = $('.tick').find('.tick-new').text();

    if (lsCurrentTick > 5003) {
        reset();
    }
}, 1000);
function stop(){
    ticker[0].stop();
}
function reset(){
    ticker[0].stop();
    ticker[0].value = 5000;
    ticker[0].start();
}

这是demo