1

我有一个包含 23 个不同 .html 文件的池,我需要随机访问它们。这部分很简单,但我需要他们在显示 40 个这些页面后链接到不同的页面。我怎样才能做到这一点?

         var startTime = new Date();
         Mousetrap.bind('e', function () {
             var endTime = new Date();
             var timeSpent = (endTime - startTime);
             alert("Correct " + timeSpent + "miliseconds");
             window.location.href = loft;
         })

          Mousetrap.bind('i', function() { 
                var endTime = new Date();
                var timeSpent = (endTime - startTime);
                $('img').css('display','block')
                alert("Incorrecto " + timeSpent + "milisegundos"); 
                })
        var loft= Math.floor((Math.random()*40)+1);

Mousetrap 是一个 js 库,它允许我将击键链接到不同的功能。这是关于反应时间的社会心理学研究。

4

1 回答 1

1

在 cookie 中设置一个计数器,以便在更改窗口位置后保持它的状态。一个用于管理 cookie 的好插件是这个家伙:https ://github.com/carhartl/jquery-cookie虽然你也可以编写一些简单的函数来设置/取消设置 cookie 像这样设置 cookie 并使用 JavaScript 获取 cookie

有这样的效果:

   var counter = $.cookie("counter"); 
    if (counter == undefined){
    counter = 0; 
    }
    var startTime = new Date();
         Mousetrap.bind('e', function () {
            if (counter < 40){
             var endTime = new Date();
             var timeSpent = (endTime - startTime);
             alert("Correct " + timeSpent + "miliseconds");
             $.cookie("counter", ++counter); 
             window.location.href = loft;
            }else{
                         //do stuff to show your thank you page
                     }
         })

          Mousetrap.bind('i', function() { 
                var endTime = new Date();
                var timeSpent = (endTime - startTime);
                $('img').css('display','block')
                alert("Incorrecto " + timeSpent + "milisegundos"); 
                })
    var loft= Math.floor((Math.random()*40)+1);
于 2013-09-04T15:03:09.747 回答