0

我在“剩余时间”字段中有一个倒计时计时器,以查找剩余时间。我还有一个字段可以输入总估计时间,并且在输入总估计时间的值后,计时器将启动。

现在我的要求是,当我更改总估计时间时,我想获取计时器的值,然后计算计时器再次启动的新值。

例如:我的第一个总估计时间 = 3,当计时器到达 02:00:00 时,我将总估计时间更改为 5,所以我的计时器将从 04:00:00 重置(逻辑:5-3+2 = 4 或 5-(3-2) = 4)

所以我想要当前的倒数计时器值。我该怎么做???

我的 Timer.js 文件:

var Timer;
var TotalSeconds;

function CreateTimer(TimerID, Time) {
Timer = document.getElementById(TimerID);
TotalSeconds = Time;

UpdateTimer()
window.setTimeout("Tick()", 1000);
}

function Tick() {
if (TotalSeconds <= 0) {
alert("Time's up!")
return;
}
TotalSeconds -= 1;
UpdateTimer()
window.setTimeout("Tick()", 1000);
}

function UpdateTimer() {
var Seconds = TotalSeconds;

var Days = Math.floor(Seconds / 86400);
Seconds -= Days * 86400;

var Hours = Math.floor(Seconds / 3600);
Seconds -= Hours * (3600);
var TimeStr = ((Days > 0) ? Days + " days " : "") + LeadingZero(Hours) + ":" +    LeadingZero(Minutes) + ":" + LeadingZero(Seconds);
Timer.innerHTML = TimeStr;
}

function  LeadingZero(Time) {

return (Time < 10) ? "0" + Time : + Time;
}

这就是我打电话给我的计时器的地方。

<script type="text/javascript" src="./Timer.js"></script>
<div id='timer'></div>
[% IF bug.estimated_time >0 %]

<script type="text/javascript">
var time;
time = CreateTimer("timer",[% bug.estimated_time %]*3600);
window.onload = CreateTimer("timer",[% bug.estimated_time %]*3600);

</script>
[% END %]

在这里我想调用函数

 <tr>
    <td align="center">
      <input name="estimated_time" id="estimated_time"
             value="[% PROCESS formattimeunit
                               time_unit=bug.estimated_time %]"
             size="6" maxlength="6" onchange="updateRemainingTime();">
      <input type = "Hidden" name="estimate_time" id="estimate_time" value="[% bug.estimated_time %]" size="6" maxlength="6">

    </td

在调用 updateRemainingTime() 时,我想获取计时器的当前值并使用它,我想使用上述逻辑重置计数器....

4

1 回答 1

1

Template Toolkit is largely irrelevant here - once the page is rendered, TT plays no further part.

Without the details of what's in Timer.js (there are dozens and dozens of Javascript timers out there), it's impossible to give you a pat answer. But presumably there is a method to obtain the current value of your timer, or interact with it to change it. And if there isn't, you need to create one.

I'm going to assume you're using this Timer, since its method names correspond with your code. If you aren't, hopefully this gets you on the way.

Basically, you need to modify one of the functions like this:

function UpdateTimer(adjustment) {
    if (typeOf adjustment != 'undefined') {
        // adjustment provided, change Timer value
        TotalSeconds += adjustment;
    } 
    Timer.innerHTML = TotalSeconds;
}

This variation allows you to optionally pass an adjustment (positive or negative) to alter the timer's value, Then you call it like this:

function updateRemainingTime() {
    var $origTime = $('#estimate_time');
    var newEstim = $('#estimated_time').val();
    UpdateTimer((newEstim - $origTime.val())*3600);
    $origTime.val(newEstim); // update hidden field to new value
}

You haven't mentioned if these values need to be stored in your database, but that's a whole different set of issues.


UPDATE

It looks like you're using a very close relation to the code I found, and what I'm suggesting you do still holds:

function UpdateTimer(adjustment) {
    var Seconds = TotalSeconds;
    if (typeOf adjustment != 'undefined') {
        // adjustment provided, change Timer value
        Seconds += adjustment;
    }
    /* rest of your UpdateTimer code remains unchanged */
} 
于 2013-06-14T01:48:12.737 回答