定时器插件在执行一次后不会工作

时间:2012-10-23 09:16:23

标签: javascript jquery jquery-plugins

单击按钮后,我使用This Timer Plugin创建计时器。您可以see it in action here.如果您注意到,当您单击任意两次购买按钮时,它将被忽略,不会再次执行。我无法弄清楚如何纠正这个问题。我需要在计时器完成后随时点击按钮重启计时器。

以下是我在第一个按钮上使用的HTML:

<h3 class="protection"><span id="sentryTime">00:00:00</span><a href="javascript:sentry()">Purchase Sentry</a></h3>

以下是我在该按钮上使用的javascript,它调用插件来创建计时器:

function sentry() {
    var dateCreated1 = new Date();
    setCharacterTime(/*hr*/0,/*min*/0,/*sec*/5,/*title*/'sentryTime',dateCreated1)} 


function setCharacterTime(hours, minutes, seconds, title, creationDate) {

    var thisDay = creationDate;
    var year = thisDay.getFullYear();
    var month = thisDay.getMonth();
    var day = thisDay.getDate();
    var aHour = thisDay.getHours();
    var aMinutes = thisDay.getMinutes();
    var aSeconds = thisDay.getSeconds();
    var aMilli = thisDay.getMilliseconds();
    thisDay = new Date(year, month, day, aHour + hours, aMinutes + minutes, aSeconds + seconds, aMilli);
    $('#' + title).countdown({until: thisDay, format: 'HMS', layout: ' {hnn}{sep}{mnn}{sep}{snn}', expiryText: 'complete '});
    ;} //end setCharacterTime() function 

如果您需要查看jQuery插件,you can find it live here.

1 个答案:

答案 0 :(得分:0)

这是我从插件作者那里收到的解决方案:

function sentry() {
    setCharacterTime('+5s', 'sentryTime');
}
function security() {
    setCharacterTime('+5m', 'securityTime');
}
function police() {
    setCharacterTime('+8m', 'policeTime');
}
function soldier() {
    setCharacterTime('+1h', 'soldierTime');
}
function warrior() {
    setCharacterTime('+1h +45m', 'warriorTime');
}
function setCharacterTime(offset, title) {
    var alertStatement = 'this is supposed to pop up once the timer is complete.';
    $('#' + title).countdown({format: 'HMS', layout: ' {hnn}{sep}{mnn}{sep}{snn}', // Initialise
        onExpiry: function() {
            alert(alertStatement);
        }
    }).countdown('option', {until: offset});// Update
}