-
-
Save Beyamor/5843270 to your computer and use it in GitHub Desktop.
$(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); }); | |
}); | |
}); |
okay i understand where responseReceived is being called i dont understand what ur accomplishing w it
what is $go.click. some callback function for clicking on the element/
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.
a) whats going on with the responses received thing here?
b) why do the request functions near bottom not need a var ?
c) is _ a special thing there ?
d) and what the hell do those request functions do anyway. are you just calling requestWords 3 times. ya you are derp.