Skip to content

Instantly share code, notes, and snippets.

@cafe4it
Created September 9, 2015 12:26
Show Gist options
  • Save cafe4it/ff0eee0c22ea8ce6193d to your computer and use it in GitHub Desktop.
Save cafe4it/ff0eee0c22ea8ce6193d to your computer and use it in GitHub Desktop.
2048
function throttle(fn, threshhold, scope) {
threshhold || (threshhold = 250);
var last,
deferTimer;
return function () {
var context = scope || this;
var now = +new Date,
args = arguments;
if (last && now < last + threshhold) {
// hold on to it
clearTimeout(deferTimer);
deferTimer = setTimeout(function () {
last = now;
fn.apply(context, args);
}, threshhold);
} else {
last = now;
fn.apply(context, args);
}
};
}
Podium = {};
Podium.keydown = function (k,w) {
var oEvent = document.createEvent('KeyboardEvent');
// Chromium Hack
Object.defineProperty(oEvent, 'keyCode', {
get: function () {
return this.keyCodeVal;
}
});
Object.defineProperty(oEvent, 'which', {
get: function () {
return this.keyCodeVal;
}
});
if (oEvent.initKeyboardEvent) {
oEvent.initKeyboardEvent("keydown", true, true, document.defaultView, false, false, false, false, k, k);
} else {
oEvent.initKeyEvent("keydown", true, true, document.defaultView, false, false, false, false, k, 0);
}
oEvent.keyCodeVal = k;
if (oEvent.keyCode !== k) {
alert("keyCode mismatch " + oEvent.keyCode + "(" + oEvent.which + ")");
}
w || (w = 1000);
setTimeout(function(){
document.dispatchEvent(oEvent);
},w);
}
var keyCodes = [
37,
38,
39,
49
]
var x = 0;
var t = setInterval(function () {
var key = keyCodes[x];
if($('.game-message').hasClass('game-over')){
clearInterval(t);
}
Podium.keydown(key,1000);
x = (x >= 3) ? 0 : ++x;
}, 1000)
@cafe4it
Copy link
Author

cafe4it commented Sep 9, 2015

function throttle(fn, threshhold, scope) {
threshhold || (threshhold = 250);
var last,
deferTimer;
return function () {
var context = scope || this;

var now = +new Date,
    args = arguments;
if (last && now < last + threshhold) {
  // hold on to it
  clearTimeout(deferTimer);
  deferTimer = setTimeout(function () {
    last = now;
    fn.apply(context, args);
  }, threshhold);
} else {
  last = now;
  fn.apply(context, args);
}

};
}
Podium = {};
Podium.keydown = function (k,w) {
var oEvent = document.createEvent('KeyboardEvent');

// Chromium Hack
Object.defineProperty(oEvent, 'keyCode', {
    get: function () {
        return this.keyCodeVal;
    }
});
Object.defineProperty(oEvent, 'which', {
    get: function () {
        return this.keyCodeVal;
    }
});

if (oEvent.initKeyboardEvent) {
    oEvent.initKeyboardEvent("keydown", true, true, document.defaultView, false, false, false, false, k, k);
} else {
    oEvent.initKeyEvent("keydown", true, true, document.defaultView, false, false, false, false, k, 0);
}

oEvent.keyCodeVal = k;

if (oEvent.keyCode !== k) {
    alert("keyCode mismatch " + oEvent.keyCode + "(" + oEvent.which + ")");
}

w || (w = 1000);
setTimeout(function(){
document.dispatchEvent(oEvent);
},w);
}
var keyCodes = [
37,
38,
39,
40
]
var x = 0;

var t = setInterval(function () {
var plusOrMinus = Math.random() < 0.5 ? -1 : 1;
keyCodes.sort(function () {
return plusOrMinus
});
var key = keyCodes[x];
if($('.game-message').hasClass('game-over')){
clearInterval(t);
}
var w = Math.floor(Math.random() * 300) + 1200;
Podium.keydown(key,w);
x = (x >= 3) ? 0 : ++x;
}, 1000)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment