×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Nicole Patten
Added: Oct 11, 2016 7:37 PM
Views: 4
Tags: no tags
  1.  var starttime;
  2.     var endtime;
  3.     var totaltime;
  4.     var keyAllowed = {};
  5.  
  6.     // fire animations on spacebar hold after 3 seconds
  7.     $(window).keydown (function(e){
  8.         //spacebar keycode
  9.         if (e.keyCode === 0 || e.keyCode === 32) {
  10.            e.preventDefault()
  11.            // prevent continuous event trigger on keydown
  12.           if (keyAllowed [e.which] === false) return;
  13.           keyAllowed [e.which] = false;
  14.  
  15.           e.preventDefault()
  16.           var d = new Date();
  17.           // log start time of keydown
  18.           starttime = d.getTime();
  19.           // start animations
  20.           textAnimation();
  21.           animateBG();
  22.           spacebarAnimate();
  23.         }
  24.     });
  25.     $(window).keyup (function(e){
  26.       keyAllowed [e.which] = true;
  27.         if (e.keyCode === 0 || e.keyCode === 32) {
  28.           e.preventDefault()
  29.           var d = new Date();
  30.           // log time of key up
  31.           endtime = d.getTime();
  32.         }
  33.         // determine total time of key press
  34.         var totaltime = endtime - starttime;
  35.         // if keypress is less than 3 seconds stop animations
  36.         if (totaltime < 2000){
  37.           stopAnimation();
  38.       }
  39.     });
  40.  
  41.     $(document).focus(function(e) {
  42.       keyAllowed = {};
  43.     });
  44.  
  45.     //fire animations on button click
  46.     $spacebarTrigger.click(function(e) {
  47.       textAnimation();
  48.       animateBG();
  49.       spacebarAnimate();
  50.     });