1
我有一個計時器用於時間跟蹤。當我點擊開始或停止時,定時器工作得很好。現在我試圖讓用戶點擊#time
標籤來手動編輯時間。一旦他們通過點擊定時器手動編輯時間並點擊開始,它應該從該「時間」開始,並且當他們點擊停止時,它應該更新隱藏的input#counter
標籤以將其轉換爲毫秒。用JS編寫的定時器手動設置時間
我的問題是我如何讓用戶手動編輯時間,所以當他們點擊開始/停止計時器的工作原理基於新的時間?例如,如果我雙擊時間並將其設置爲10:00:00並按下開始,它應該從10:00:00開始,否則它會更新隱藏的輸入,因此當我單擊提交時,它會保存更新的時間
這裏是小提琴:https://jsfiddle.net/4pu3x62g/
這裏是HTML:
<div class="form-group timer">
<span id="time"></span>
</div>
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-6">
<input class="btn btn-success col-md-12 col-sm-12 col-xs-12" type="button" value="start" onclick="start();">
</div>
<div class="col-md-6 col-sm-12 col-xs-6">
<input class="btn btn-danger col-md-12 col-sm-12 col-xs-12" type="button" value="stop" onclick="stop();">
</div>
</div>
<input type="hidden" value="" id="counter" name="counter" />
這裏是定時器JS
var clsStopwatch = function() {
var startAt = 0;
var lapTime = 0;
var now = function() {
return (new Date()).getTime();
};
this.start = function() {
startAt = startAt ? startAt : now();
};
this.stop = function() {
lapTime = startAt ? lapTime + now() - startAt : lapTime;
startAt = 0;
};
this.time = function() {
return lapTime + (startAt ? now() - startAt : 0);
};
};
var x = new clsStopwatch();
var $time;
var clocktimer;
function pad(num, size) {
var s = "0000" + num;
return s.substr(s.length - size);
}
function formatTime(time) {
var h = m = s = ms = 0;
var newTime = '';
h = Math.floor(time/(3600 * 1000));
time = time % (3600 * 1000);
m = Math.floor(time/(60 * 1000));
time = time % (60 * 1000);
s = Math.floor(time/1000);
ms = time % 1000;
newTime = pad(h, 2) + ':' + pad(m, 2) + ':' + pad(s, 2);
//newTime = pad(h, 2) + ':' + pad(m, 2) + ':' + pad(s, 2) + ':' + pad(ms, 2);
return newTime;
}
function show() {
$time = document.getElementById('time');
update();
}
function update() {
$time.innerHTML = formatTime(x.time());
}
function start() {
clocktimer = setInterval("update()", 1);
x.start();
}
function millisecondsToHours(amountMS) {
return amountMS/3600000;
}
function stop() {
x.stop();
document.getElementById('counter').value = millisecondsToHours(x.time());
clearInterval(clocktimer);
}
這裏是使跨度#時可編輯插件:
//plugin to make any element text editable
$.fn.extend({
editable: function() {
$(this).each(function() {
var $el = $(this),
$edittextbox = $('<input type="text"></input>').css('min-width', $el.width()),
submitChanges = function() {
if ($edittextbox.val() !== '') {
$el.html($edittextbox.val());
$el.show();
$el.trigger('editsubmit', [$el.html()]);
$(document).unbind('click', submitChanges);
$edittextbox.detach();
}
},
tempVal;
$edittextbox.click(function (event) {
event.stopPropagation();
});
$el.dblclick(function (e) {
tempVal = $el.html();
$edittextbox.val(tempVal).insertBefore(this)
.bind('keypress', function (e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) {
submitChanges();
}
}).select();
$el.hide();
$(document).click(submitChanges);
});
});
return this;
}
});
這裏就是事件觸發:
//implement editable plugin
$('span#time').editable().on('editsubmit', function (event, val) {
//Need to trigger the timer with the new val from here
});
這是偉大的,但它缺少#counter隱藏輸入標籤。當按下提交時,#counter的值是發送到數據庫的值。這是時間轉換爲小時。例如01:00:00會給#counter值1.0 - 這是用於發票的時間給它一些透視。有什麼機會重新加入? – Derek
您可以詳細闡述一下您的關於如何綁定隱藏的#counter輸入標籤的示例嗎?當按下停止按鈕時,它會計算時間字段00:00:00中的內容,以0.0000作爲小時小數,用於記錄跟蹤時間。 – Derek
也許明天...... – WaldemarIce