Commit 6524623b authored by System Administrator's avatar System Administrator Committed by Vitaly Lipatov

Improve timer

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