1

I have a pool of 23 different .html files, and I need to access them randomly. That part was easy, but I need them to link to a different page after 40 of these pages have been shown. How can I do this?

         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 is a js library that alows me to link key strokes to different functions. This is a social psycology study on reaction time.

  • 1
    Your question isn't clear. Can you please elaborate on what the goal is? – Brad Sep 04 '13 at 14:46
  • I am doing an experiment online where i have 23 different stimuli that i have written onto .html files. In total I have 23 of them. I need them to be in different order each time I access the experiment, but I only need them to appear 40 times (I know that means some of them will come out more than once). After the 40th stimuli, I need to link to a “thank you page” and the experiment to stop. – Alejandro Reinel Sep 04 '13 at 14:50
  • Still not too sure if you're talking 40 page hits total by all users or by each user. If it's 40 per user, then you could use a session variable and increment it on each page load, if it's 40 total between all users, use cache instead. – user2366842 Sep 04 '13 at 14:54
  • Each user must complete the experiment that consists of 40 different “pages”, and then go to a “thank you page”. – Alejandro Reinel Sep 04 '13 at 14:56
  • Session variable would do it then, or you can go the route described below using a cookie. The only down side to that is there's always the possibility that someone could turn cookies off, unless this is in some sort of controlled environment where that could be prevented. Not sure which server side language you're using, php or asp.net, if you're not using either of these, cookies may be the better answer. – user2366842 Sep 04 '13 at 15:30

1 Answers1

1

Set a counter in a cookie so you can keep state of it after you change the window location. A good plugin to use for managing cookies is this guy: https://github.com/carhartl/jquery-cookie though you could also write some simple functions to set / unset cookies like so Set cookie and get cookie with JavaScript

Something to this effect:

   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);
Community
  • 1
  • 1
Brad
  • 6,106
  • 4
  • 31
  • 43
  • I downloaded the plugin, but i have no clue why it does not work ... i keep getting this in the console : Uncaught TypeError: Object function ( selector, context ) { // The jQuery object is actually just the init constructor 'enhanced' return new jQuery.fn.init( selector, context, rootjQuery ); } has no method 'cookie' I refenrenced the jquery.cookie.js file in my html but still does not work – Alejandro Reinel Sep 04 '13 at 15:57
  • The problem is that you've added the cookie script after As a result, myscript.js runs before the cookie plugin does so $.cookie does not exist yet. – Brad Sep 04 '13 at 16:20