【发布时间】:2015-12-04 06:14:29
【问题描述】:
所以我已经坚持了一段时间。如果有人可以帮助我解决这个问题,我将不胜感激!我想要的是让我的函数及其回调在继续循环之前完成。在用户在文本区域中输入英文文本并单击翻译按钮后,我调用“translate_all”。 (“方法”,“g_loop”,“g_where”和“go”是全局变量。)最终我想要的是让输入的英文短语翻译成数组中的下一种语言,然后把它翻译回英文看看如果有差异,然后继续浏览所有语言。
我尝试过承诺,但老实说,我无法理解它。我尝试了这种“hackish”方法,将循环捕获在一个while循环中,直到全局变量“go”在回调之后发生变化。但是,发生的事情是当我运行它时浏览器冻结,我认为这是因为虽然该方法阻止循环继续,但它是如此卡在循环中,以至于它永远不会从回调中接收到“go”已更改的消息.也许我可以像很快向我推荐的那样做某种事件监听器,但我不确定。我觉得我是如此接近,它让我疯狂地试图让它工作这么长时间。任何和所有的帮助表示赞赏!谢谢!
我有这个:
var translate_all = function translate_all () {
// set global g_where to this div to append an image
g_where = $("#g_div");
g_where.append("<img alt='Google Logo' src='/img/google_g_medium.png'/><br>");
// get the text the user input
var a_text = $("#accuracy_text").val();
// translation codes for api
var language_codes = ["en", "ar", "bg", "ca", "hr", "cs", "da", "nl", "et", "fi", "fr", "de", "el", "ht", "id", "it", "ko", "lv", "lt", "ms", "mt", "no", "fa", "pl", "pt", "ro", "ru", "sl", "sl", "es", "th", "tr", "uk", "ur", "vi"];
// set global g_loop to what text the user input
g_loop = a_text;
// start a loop
for (var i = 1; i < language_codes.length; i++)
{
// error checking and trying to debug and see what is wrong
console.log("working")
// define which callback i want to run
method = 2;
// this callback sets the response from google placed in g_response to g_loop; it translates from one language in loop to the next
GoogleTranslate(g_loop, language_codes[i-1], language_codes[i]);
console.log("continue");
// THIS is where i try to get the loop to stop until the callback has run because this loop is only able to break when the callback has finished
while (go == 0)
{
if (go == 1)
{
break;
}
}
// set go back to zero
go = 0;
// set g_where and append the response to the div
g_where = $("#g_div");
g_where.append(g_response)
method = 1;
// this translates the initial response back to english to see if there is a difference and appends it to the div
GoogleTranslate(g_loop, language_codes[i], "en");
// trying to make sure the callback is executed again before continuing
while (go == 0)
{
if (go == 1)
{
break;
}
}
go = 0;
}
}
function GoogleTranslate (text, from, to) {
var newScript = document.createElement('script');
var sourceText = escape(text);
newScript.type = 'text/javascript';
var APIKEY = 'my api key';
var source = 'https://www.googleapis.com/language/translate/v2?';
source += 'key=' + APIKEY;
source += '&source=' + from;
source += '&target=' + to;
source += '&callback=google_translation';
source += '&q=' + sourceText;
newScript.src = source;
console.log("sent");
// send the reuqest to google api
$('head')[0].appendChild(newScript);
}
// callback
function google_translation(response) {
g_response = response.data.translations[0].translatedText;
if (method == 0)
{
// replaces text in a given location (not for code shown)
g_where.val(g_response);
console.log("0");
}
if (method == 1)
{
// appends text in a given location
g_where.append(": " + g_response + "<br>");
console.log("1");
}
if (method == 2)
{
// sets variable to response
g_loop = g_response;
console.log("2");
}
go = 1;
console.log("translated");
}
【问题讨论】:
标签: javascript loops callback google-api google-translate