0

我正在做一个交互式测验,我遇到了一个问题。我需要为其添加某种时间限制(假设为 5s 用于测试目的),这样当时间用完时,无论用户处于哪个阶段,测验都应该跳到结果部分并将所有未回答的问题计为错误的。

这是我的代码:

jQuery(function(){
    var tourismQuiz = {
        answers: { q1: 'd', q2: 'd', q3: 'a', q4: 'c', q5: 'a', q6: 'd', q7: 'd', q8: 'a', q9: 'c', q10: 'a' },
        questionLenght: 10,
        checkAnswers: function() {
            var arr = this.answers;
            var ans = this.userAnswers;
            var resultArr = []
            for (var p in ans) {
                var x = parseInt(p) + 1;
                var key = 'q' + x;
                var flag = false;
                if (ans[p] == 'q' + x + '-' + arr[key]) {
                    flag = true;
                }
                else {
                    flag = false;
                }
                resultArr.push(flag);
            }
            return resultArr;
        },
        init: function(){
            jQuery('.btnNext').click(function(){
                if (jQuery('input[type=radio]:checked:visible').length == 0) {

                    return false;
                }
                jQuery(this).parents('.questionContainer').fadeOut(500, function(){
                    jQuery(this).next().fadeIn(500);
                });
                var el = jQuery('#progress');
                el.width(el.width() + 60 + 'px');
            });
            jQuery('.btnPrev').click(function(){
                jQuery(this).parents('.questionContainer').fadeOut(500, function(){
                    jQuery(this).prev().fadeIn(500)
                });
                var el = jQuery('#progress');
                el.width(el.width() - 60 + 'px');
            })
            jQuery('.btnShowResult').click(function(){
                var arr = jQuery('input[type=radio]:checked');
                var ans = tourismQuiz.userAnswers = [];
                for (var i = 0, ii = arr.length; i < ii; i++) {
                    ans.push(arr[i].getAttribute('id'))
                }
            })
            jQuery('.btnShowResult').click(function(){
                jQuery('#progress').width(300);
                jQuery('#progressKeeper').hide();
                var results = tourismQuiz.checkAnswers();
                var resultSet = '';
                var trueCount = 0;
                for (var i = 0, ii = results.length; i < ii; i++){
                    if (results[i] == true) {
                        trueCount = trueCount + 2;
                    } else {
                        trueCount = trueCount - 1;
                    }
                    resultSet += '<div> Question ' + (i + 1) + ' is ' + results[i] + '</div>'

                    if (trueCount <= 5) {
                        jQuery("body").css("background-color", "red");
                    } else {
                        jQuery("body").css("background-color", "green");
                    }
                }

                resultSet += '<div class="totalScore">Your total score is ' + trueCount + ' / 20</div>'
                jQuery('#resultKeeper').html(resultSet).show();
            })
        }
    };
    tourismQuiz.init();
})

因此,与其等待用户按下.btnShowResult完成测试,不如在时间用完时执行按下此按钮执行的功能。

4

3 回答 3

1

Checkout window.setTimeout,这应该让你开始:在指定的延迟后调用函数或执行代码片段。

因此,您希望给用户一些时间(5000 毫秒)来回答问题,然后再将其转发到下一个问题,例如:

//Psuedocode:

var askNextQuestion, timer ;

askNextQuestion = function(){
 // ... render the question or do whatever
 timer = setTimeout(askNextQuestion, 5000);
 // .. do any clean up and check to see if it's the last question etc.
};

askNextQuestion();

我不会尝试比 Mozilla 更好地解释它! https://developer.mozilla.org/en/docs/DOM/window.setTimeout

于 2013-03-19T11:37:09.273 回答
0

Use setTimeout as last statement of your init function and write the functionality binded with jQuery('.btnShowResult'). in a separate function add it in setTimeout

setTimeout("show_result", 20000);

Also, use for instance .clearTimeout(timeoutVariable) to reset the timeout if the user presses the button within the time limit.

于 2013-03-19T11:34:09.677 回答
0

保存提出问题时的时间戳,然后将其与用户提交答案时的时间戳进行比较。如果比较的时间超过限制,则答案已提交迟到。

倒计时的呈现可以只是图形的,例如。条形宽度从 100% 到 0% 的 CSS3 动画。以秒为单位交换“慢”为您的时间限制。

$("#bar").animate({ width:'0%' },'slow');

查看http://www.w3schools.com/css3/css3_animations.asp以获取有关设置动画的提示!

于 2013-03-19T11:34:33.847 回答