Tuesday, 13 August 2013

JQuery function return variable "undefined"

JQuery function return variable "undefined"

I'm trying to grab a random word from a dictionary file using JQuery - but
I'm not able to 'return' the value. The returned value is always
"undefined".
function RandomWord()
{
$.get('dictionary.txt', function(data) {
var words = data.split("\n");
var idx = Math.floor(words.length * Math.random());
word = words[idx];
return word;
});
}
However, when I replace "return word;" with "alert(word);" - it does show
me the random word.
Any idea how I can fix this?

No comments:

Post a Comment