问卷倒计时脚本

时间:2022-01-03 22:04:40
$(function () {
    var submitTimerWrapper = $('.submit-timer-wrapper');
    var submitTimerDom = $('#submit-timer');
    var remainSec = 3 * 60;
    function fillZero(num) {
        return num < 10 ? '0' + num : '' + num;
    }
    function formatDate() {
        var remainHours = parseInt(remainSec / (60 * 60));
        var remainMinutes = parseInt((remainSec - 60 * 60 * remainHours) / 60);
        var remainSeconds = remainSec - 60 * 60 * remainHours - 60 * remainMinutes;
        return fillZero(remainHours) + ':' + fillZero(remainMinutes) + ':' + fillZero(remainSeconds);
    }
    var submitTimer = setInterval(function () {
        if (remainSec == 60) {prompt
            submitTimerWrapper.addClass('only');
            submitTimerWrapper.find('.prompt').text('答题时间仅剩');
        }
        submitTimerDom.html(formatDate());
        if (remainSec == 0) {
            clearInterval(submitTimer);
            $('#form1').submit();
        }
        remainSec--;
    }, 1000);
});