Created
June 23, 2013 00:43
-
-
Save Beyamor/5843270 to your computer and use it in GitHub Desktop.
Twitter name generator
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
$(function() { | |
var adjectives = [], | |
nouns = []; | |
var any = function(coll) { | |
var index = Math.floor(Math.random() * coll.length); | |
return coll[index]; | |
}; | |
var generateName = function() { | |
var adjective, noun; | |
do { | |
adjective = any(adjectives); | |
noun = any(nouns); | |
} while (adjective == noun); | |
return adjective.toUpperCase() + ' ' + noun.toUpperCase(); | |
}; | |
var responsesReceived = 0, | |
totalNumberOfResponses = 3, | |
responseReceived = function() { | |
++responsesReceived; | |
if (responsesReceived == totalNumberOfResponses) { | |
var $go = $('<button type="button">Go</button>') | |
$go.click(function() { | |
$('#name').html(generateName()); | |
}); | |
$('#loading').remove(); | |
$('body').append($go); | |
} | |
}; | |
var requestWords = function(url, process) { | |
$.ajax( | |
url, { | |
dataType: 'text', | |
success: function(results) { | |
var words = results | |
.split(/\n/) | |
.filter(function(word) { return (word.length > 0); }); | |
process(words); | |
responseReceived(); | |
}, | |
error: function() { | |
alert('Something done broke'); | |
} | |
}); | |
}; | |
requestWords('adjectives', function(words) { | |
$.each(words, function(_, word) { adjectives.push(word); }); | |
}); | |
requestWords('nouns', function(words) { | |
$.each(words, function(_, word) { nouns.push(word); }); | |
}); | |
requestWords('both', function(words) { | |
$.each(words, function(_, word) { adjectives.push(word); nouns.push(word); }); | |
}); | |
}); |
you remove loading but where is that even added
looks like the callback expects multiple words? splittong on /n so a list of em okay
looks like the callback expects multiple words? splittong on /n so a list of em okay
are these success and error tags some sort of generic thing going on here?
nanananananananna batman
generic as in like, i dunno... you can set em up for different purposes for libraries, as opposed to hard coded try catch in python. maybe a colton library could have like
chince:
//
dismember:
//
fart:
//
or no im dumb those aren't labels they are dict items passed to the ajax ?
hope you dont get like 12 emails.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what is $go.click. some callback function for clicking on the element/