Here is a code snippet
var translatedWord = "";
currentCharacter = "猫";
translateRequestURL = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=zh-CN&tl=en&dt=t&q=' + currentCharacter;
$.ajax(translateRequestURL).done(function (data) {
translatedWord = data[0][0][0]; //this is what I need
alert('This works '+translatedWord); //this works
});
alert('this does not '+translatedWord); //this doesn't work
Here is a fiddle if the code above https://jsfiddle.net/ovntx1c1/
I basically need to get the translatedWord (cat, in this case) to be able to use after the function translates it. I am seeing from the fiddle that my alert outside the function kicks off first, I imagine that's because I am not actually calling the function so functions run last, which is probably my issue.
I tried changing "function (data)" to "function someFunction (data)" so its named and then afterwards call testing(), but I didn't exactly know what to put in the parenthesis. I also tried to "return translatedWord" at the end of the function.
I made a fiddle for that attempt: https://jsfiddle.net/ovntx1c1/2/
I know this has been posted more than a few times and I run the risk of being downvoted but I just can't figure out what I am doing wrong.