Commit 2368dcaa authored by System Administrator's avatar System Administrator

Improve timer

parent 1c6d6ad2
//Время старта скрипта
var timeStart = new Date().getTime();
//Номер потока
var timeout_id = 0;
......@@ -25,26 +23,45 @@ var changed_remain = 0;
//Создание баги
var etersoft_create = 0;
// Время начала работы над багом
var startTime;
// Время, когда таймер поставили на паузу
var pauseStartTime;
function initTimer(){
var storedValue = parseInt(getStorage(storage_key));
var storedPauseValue = parseInt(getStorage(storage_key+'p'));
if(storedValue){
startTime = storedValue;
if(storedPauseValue){
control_timer = 1;
pauseStartTime = storedPauseValue;
setPauseDisplay(true);
}
} else {
// Если сохраненного значения нет, то принимаем текущее время за начало работы
startTime = new Date().getTime();
// Запишем его
setStorage(storage_key, startTime);
}
updateTimer();
// Поставим обновление таймера каждую секунду
setInterval(updateTimer, 1000);
}
//Возращает прошедшее время в секундах с учетом пауз
// TODO: переименовать
function initStopwatch() {
var timeNow = new Date().getTime();
if (getStorage(storage_key) != 0) {
if (getStorage(storage_key) == 'unknown') {
var timeDiff = 0.125;
} else {
var timeDiff = getStorage(storage_key);
}
if(control_timer){
return (pauseStartTime - startTime)/1000;
} else {
var timeDiff = timeNow - timeStart - diff_time_pause;
return (new Date() - startTime)/1000;
}
return (timeDiff/1000);
}
//Парсинг времени в формат чч:мм:сс
function getSecs() {
function updateTimer() {
var mySecs = initStopwatch();
var myMinutes = Math.floor(mySecs/60);
var myHours = Math.floor(myMinutes/60);
......@@ -63,12 +80,9 @@ function getSecs() {
}
document.querySelector("#timerblock #timespent").value = myHours + ":" + myMinutes1 + ":" + mySecs1;
setStorage(storage_key, mySecs*1000+1000);
timeout_id = window.setTimeout('getSecs()', 1000);
}
//Установка в поле отработанного времени в минутах
function setWorkTime(manualTime) {
var secs = initStopwatch();
......@@ -77,32 +91,36 @@ function setWorkTime(manualTime) {
document.querySelector("#timeQuestionDiv #realworktime").value = (!manualTime) ? minutes : manualTime;
}
function setPauseDisplay(pause){
document.querySelector("#timerblock #timespent").style.color = pause ? "gray" : "black";
document.querySelector("#timerblock #timer_pause").style.visibility = pause ? "hidden" : "visible";
document.querySelector("#timerblock #timer_play").style.visibility = pause ? "visible" : "hidden";
}
//Пауза/продолжить
function controlTimer() {
if (control_timer == 0) {
time_pause = new Date().getTime();
if (control_timer === 0) {
pauseStartTime = new Date().getTime();
setStorage(storage_key+'p', pauseStartTime);
control_timer = 1;
clearTimeout(timeout_id);
document.querySelector("#timerblock #timespent").style.color = "gray";
document.querySelector("#timerblock #timer_pause").style.visibility = "hidden";
document.querySelector("#timerblock #timer_play").style.visibility = "visible";
setPauseDisplay(true);
} else {
diff_time_pause += new Date().getTime() - time_pause;
setStorage(storage_key+'p', 'unknown');
startTime += (new Date() - pauseStartTime);
setStorage(storage_key, startTime);
control_timer = 0;
window.setTimeout('getSecs()', 1000);
document.querySelector("#timerblock #timespent").style.color = "black";
document.querySelector("#timerblock #timer_pause").style.visibility = "visible";
document.querySelector("#timerblock #timer_play").style.visibility = "hidden";
setPauseDisplay(false);
}
}
//Сброс таймера
function resetTimer() {
setStorage(storage_key, 'unknown');
diff_time_pause = new Date().getTime() - timeStart;
setStorage(storage_key, new Date().getTime());
setStorage(storage_key+'p', control_timer ? new Date().getTime() : 'unknown');
pauseStartTime = startTime = new Date().getTime();
updateTimer();
}
/////////////////////////////////////////////////////
......@@ -114,10 +132,6 @@ function closeDiv() {
document.querySelector("#timeQuestionDiv").style.display = "none";
}
function initWatch() {
timeStart = new Date().getTime();
}
function getStorage(key) {
if (window['sessionStorage'] != null) {
if (!sessionStorage.getItem(key)) {
......@@ -283,11 +297,11 @@ window.setTimeout('istimer()', 500);
if (typeof (window.addEventListener) != 'undefined') {
//gecko, safari, konqueror and standard
window.addEventListener('load', getSecs(), false);
window.addEventListener('load', initTimer, false);
} else if (typeof document.addEventListener != 'undefined') {
//opera 7
document.addEventListener('load', getSecs(), false);
document.addEventListener('load', initTimer, false);
} else if(typeof window.attachEvent != 'undefined') {
//win/ie
window.attachEvent('onload', getSecs());
window.attachEvent('onload', initTimer);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment