这是同步问题吗?使用谷歌翻译来链式翻译单词。
我正在尝试编写一个脚本,让我能够快速检查一个单词在被谷歌翻译多次翻译后如何变化。不幸的是,我对 Javascript 的经验不是很丰富,我无法确定问题的根源:
function initialize() {
var word = "Hello";
var english = [word];
var german = [];
document.write("1");
var i = 0;
for (i=0; i<10; i++) {
google.language.translate(english[i], 'en', 'de', function(result) {
if (!result.error) {
german.push(result.translation);
document.write(result.translation);
}
else {
document.write(result.error.message);
}
document.write("2");
});
document.write("3");
google.language.translate(german[i], 'de', 'en', function(result) {
if (!result.error) {
english.push( result.translation );
document.write ( result.translation );
}
else {
document.write(result.error.message);
}
document.write("4");
});
document.write("5");
}
}
google.setOnLoadCallback(initialize);
如您所见,对 google.language.translate 的第二次调用将第一次的结果作为参数称呼。我希望在运行时在文档中看到类似的内容:
1Hallo23Hello45Hallo23Hello45Hallo23Hello45 ...
相反,我得到 13Hallo2 和崩溃。因为它打印 1 和 3 (即它在执行所有第一个翻译调用之前执行 doc.write("3") ),所以我怀疑正在发生某种异步行为。我使用的代码按照我编写的顺序执行!帮助!理想情况下,我想知道如何让循环的其余部分仅在第一次调用谷歌翻译返回后执行。
I'm trying to write a script that will allow me to quickly examine how a word changes when translated multiple times by Google translate. Unfortunately, I'm not very experienced with Javascript and I can't pin down the source of the problem I'm having:
function initialize() {
var word = "Hello";
var english = [word];
var german = [];
document.write("1");
var i = 0;
for (i=0; i<10; i++) {
google.language.translate(english[i], 'en', 'de', function(result) {
if (!result.error) {
german.push(result.translation);
document.write(result.translation);
}
else {
document.write(result.error.message);
}
document.write("2");
});
document.write("3");
google.language.translate(german[i], 'de', 'en', function(result) {
if (!result.error) {
english.push( result.translation );
document.write ( result.translation );
}
else {
document.write(result.error.message);
}
document.write("4");
});
document.write("5");
}
}
google.setOnLoadCallback(initialize);
As you can see, the second call to google.language.translate takes as an argument the result of the first call. I expect to see something like this in the document when this is run:
1Hallo23Hello45Hallo23Hello45Hallo23Hello45 ...
Instead I get 13Hallo2 and a crash. Because it prints 1and 3 (ie it executes the doc.write("3") before it executes all of the first call to translate) I suspect some sort of asynchronous behavior is going on. I'm used code that executes in the order I wrote it! Help! Ideally I'd like to know how I can get the rest of the loop to only execute after the first call to google translate has returned.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这确实是一个同步问题。当您调用第一个
google.language.translate
时,您会传入一个回调,但该回调不会立即执行 - 仅在translate
从服务器获取翻译后执行。与此同时,代码继续运行,并执行第二个google.language.translate
,传入german[i]
,此时该值可能尚未定义。要序列化这些异步调用的执行,您可以在第一个调用的回调中对
google.language.translate
进行第二次调用,位于document.write("2" )
。这将确保在使用之前设置german[i]
。That's indeed a synchronization issue. When you call the first
google.language.translate
, you pass in a callback, but that callback isn't executed immediately - only aftertranslate
gets a translation from the server. In the mean time, the code continues to run, and executes the secondgoogle.language.translate
, passing ingerman[i]
, which is probably undefined at that point.To serialize the execution of those a-sync calls, you can place the second call to
google.language.translate
inside the callback of the first one, after thedocument.write("2")
. This will ensuregerman[i]
is set before used.