Skip to content

Instantly share code, notes, and snippets.

@ToeJamson
Last active December 18, 2015 07:49
Show Gist options
  • Save ToeJamson/5749993 to your computer and use it in GitHub Desktop.
Save ToeJamson/5749993 to your computer and use it in GitHub Desktop.
Trackpad Destroyer
// TIMER
// CREDITS : http://houtz.tv/javascript/timer.html
function start(){
var seconds = 30;
var millisec = 0;
var timer;
function display(){
if (millisec <= 0){
millisec=9;
seconds-=1;
publish(pos);
}else
millisec-=1;
if (seconds<=0 && millisec<=0 ){
end();
}else
timer = setTimeout(display, 100);
$('#count-down').html(seconds+'.'+millisec);
}
function end(){
clearTimeout(timer);
whowin();
}
function whowin(){
if (pos > other){
$('#score-board').html("<div class=result>You Win ;)</div>")
return;
}
if (pos < other){
$('#score-board').html("<div class=result>You Lose ;(</div>")
return;
}
else{
$('#score-board').html("<div class=result>Tie, LOL</div>")
return;
}
console.log('pos', pos, 'other', other);
}
display();
}
<div id=screen><div>
// UTILS
function supplant( str, values ) {
return str.replace( /{([\w\-]+)}/g, function( _, match ) {
return values[match] || _
} );
}
// TIERS
var tiers = []
var tier ='<div class=tier style="background-color:{color}"></div>'
var colors = ["#ff4646", "#0863bf", "#3d4c6b", "#ec9319",
"#7d9906", "#dd641f", "#8dc050", "#f18822",
"#8fb037", "#785A3A", "#E7A800", "#1A8FC9",
"#681D2A", "#af4009", "#0f8c98", "#f18822",
"#005eac", "#f64000", "#3b5998"];
for(var i=0; i<10000; i++){
var color = colors[Math.floor(Math.random()*colors.length)];
var _tier = supplant(tier, {'color': color});
tiers.push(_tier);
}
$(function(){
$('#screen').html(tiers.join(''));
start();
});
var other = 0;
var pos=0;
// GET SCROLL POSITION
$window = $(window);
$window.scroll(function(){
pos = $window.scrollTop();
$('#scrolled-me').html(pos);
});
var pubnub = PUBNUB.init({
publish_key : 'PUBLISH KEY HERE',
subscribe_key : 'SUBSCRIBE KEY HERE'
});
function publish(m){
pubnub.publish({
channel:'trackpad-destroyer-1',
message: m
});
pubnub.subscribe({
channel: 'trackpad-destroyer-2',
message: update
});
// CALLBACK FOR SUBSCRIBE
function update(m){
other = m;
$('#scrolled-other').html(m);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment