Last active
December 16, 2015 17:59
-
-
Save mkantor/5474399 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
progress { | |
display: block; | |
} | |
.winner { | |
background-color: lime; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<progress class="racer1" value="0"></progress> | |
<progress class="racer2" value="0"></progress> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Racer = function() {}; | |
Racer.prototype = { | |
position: 0, | |
move: function(scale) { | |
if(scale === undefined) { | |
scale = 1; | |
} | |
var distance = Math.random(); | |
this.position += distance * scale; | |
return this.position; | |
} | |
}; | |
document.addEventListener('DOMContentLoaded', function() { | |
var racer1 = new Racer(); | |
var racer2 = new Racer(); | |
var racer1Progress = document.querySelector('progress.racer1'); | |
var racer2Progress = document.querySelector('progress.racer2'); | |
var stepScale = 1 / 100; | |
var step = window.setInterval(function() { | |
racer1Progress.value = racer1.move(stepScale); | |
racer2Progress.value = racer2.move(stepScale); | |
if(racer1Progress.position >= 1) { | |
racer1Progress.classList.add('winner'); | |
window.clearInterval(step); | |
} | |
if(racer2Progress.position >= 1) { | |
racer2Progress.classList.add('winner'); | |
window.clearInterval(step); | |
} | |
}, 10); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: "Racers" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://jsfiddle.net/gh/gist/library/pure/5474399/