diff options
Diffstat (limited to 'addons/survey/static/src/js/survey_timer.js')
| -rw-r--r-- | addons/survey/static/src/js/survey_timer.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/addons/survey/static/src/js/survey_timer.js b/addons/survey/static/src/js/survey_timer.js new file mode 100644 index 00000000000..bb7af08104f --- /dev/null +++ b/addons/survey/static/src/js/survey_timer.js @@ -0,0 +1,45 @@ +odoo.define('survey.timer', function (require) { +'use strict'; + +require('web.dom_ready'); + +if (!$('.js_survey_timer').length) { + return $.Deferred().reject("DOM doesn't contain '.js_survey_timer'"); +} + +var $parent = $('.js_survey_timer'); +var timeLimitMinutes = parseInt($parent.data('time_limit_minutes')); + +if (timeLimitMinutes <= 0) { + return $.Deferred().reject("Timer is not positive"); +} + +var countDownDate = moment.utc($parent.data('timer')).add(timeLimitMinutes, 'minutes'); + +if (countDownDate.diff(moment.utc(), 'seconds') < 0) { + return $.Deferred().reject("Timer is already finished"); +} + +var formatTime = function (time) { + return time > 9 ? time : '0' + time; +}; + +var $timer = $parent.find('.timer'); +var interval = null; +var updateTimer = function () { + var timeLeft = countDownDate.diff(moment.utc(), 'seconds'); + + if (timeLeft >= 0) { + var timeLeftMinutes = parseInt(timeLeft / 60); + var timeLeftSeconds = timeLeft - (timeLeftMinutes * 60); + $timer.html(formatTime(timeLeftMinutes) + ':' + formatTime(timeLeftSeconds)); + } else { + clearInterval(interval); + $('.js_surveyform').submit(); + } +}; + +updateTimer(); +interval = setInterval(updateTimer, 1000); + +}); |
