Last active
August 29, 2015 14:12
-
-
Save sofish/bc0774daf3b0e3def25b to your computer and use it in GitHub Desktop.
CSS Conf 投资
This file contains hidden or 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 range = function(num) { | |
return Array(num).fill(1).reduce(function(ret, item, i) { | |
return ret.concat(i + 1); | |
}, []); | |
}; | |
var select = function(num) { | |
var ID = '___list'; | |
var list = window[ID] || localStorage.getItem(ID); | |
if(typeof list === 'string') list = JSON.parse(list); | |
if(!list) { | |
num = +num; | |
if(isNaN(num)) throw '人数只能是数字'; | |
list = range(num); | |
} | |
window.addEventListener('beforeonload', function() { | |
if(list.slice) localStorage.setItem(ID, JSON.stringify(list)); | |
}); | |
var random = Math.floor(Math.random() * list.length); | |
var lucky = list[random]; | |
list.splice(random, 1); | |
window[ID] = list; | |
return lucky; | |
}; | |
select(100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment