Skip to content

Instantly share code, notes, and snippets.

@sofish
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sofish/bc0774daf3b0e3def25b to your computer and use it in GitHub Desktop.
Save sofish/bc0774daf3b0e3def25b to your computer and use it in GitHub Desktop.
CSS Conf 投资
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