【问题标题】:Random .HTML, how to make it stop随机的.HTML,如何让它停止
【发布时间】:2013-09-08 03:26:06
【问题描述】:

我有一个包含 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 库,它允许我将击键链接到不同的功能。这是关于反应时间的社会心理学研究。

【问题讨论】:

  • 您的问题不清楚。您能否详细说明目标是什么?
  • 我在网上做一个实验,我有 23 种不同的刺激,我已经写入 .html 文件。我总共有 23 个。每次我访问实验时,我都需要它们以不同的顺序出现,但我只需要它们出现 40 次(我知道这意味着它们中的一些会出现不止一次)。在第 40 次刺激之后,我需要链接到“感谢页面”并停止实验。
  • 仍然不太确定您说的是所有用户还是每个用户的 40 个页面点击总数。如果每个用户是 40,那么您可以使用会话变量并在每次页面加载时递增它,如果所有用户之间的总数为 40,请改用缓存。
  • 每个用户必须完成包含 40 个不同“页面”的实验,然后进入“感谢页面”。
  • Session 变量会执行此操作,或者您可以使用 cookie 走下面描述的路线。唯一的缺点是总有可能有人可以关闭 cookie,除非这是在某种可以防止的受控环境中。不确定您使用的是哪种服务器端语言,php 或 asp.net,如果您不使用其中任何一种,cookie 可能是更好的答案。

标签: javascript jquery random


【解决方案1】:

在 cookie 中设置一个计数器,以便在更改窗口位置后保持它的状态。一个用于管理 cookie 的好插件是这个人:https://github.com/carhartl/jquery-cookie 虽然您也可以编写一些简单的函数来设置/取消设置 cookie,例如 Set cookie and get cookie with JavaScript

大意是这样的:

   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);

【讨论】:

  • 我下载了插件,但我不知道为什么它不起作用......我在控制台中不断收到这个:Uncaught TypeError: Object function (selector, context) { // jQuery 对象实际上只是 init 构造函数 'enhanced' return new jQuery.fn.init( selector, context, rootjQuery );没有方法 'cookie' 我在我的 html 中引用了 jquery.cookie.js 文件,但仍然无法正常工作
  • 问题是你在 之后添加了cookie脚本,结果, myscript.js 在 cookie 插件之前运行,所以 $.cookie 还不存在。
猜你喜欢
  • 2012-07-14
  • 1970-01-01
  • 2011-03-05
  • 1970-01-01
  • 2011-11-08
  • 2019-12-12
  • 2011-04-02
  • 1970-01-01
  • 2014-12-15
相关资源
最近更新 更多