我可以在 Google Loader 运行后运行 JavaScript 函数吗?
我正在使用 google.load()
加载 Google API,我需要处理它构建的一些内容,但我需要在 JavaScript 完全加载后运行它,有没有办法确保这种情况发生?
这是我构建图像列表的方法,但我需要向每个 img
标签添加一个属性,直到构建正确之后才能做到这一点?
google.load("feeds", "1");
function initialize() {
var feed = new google.feeds.Feed("myfeed.rss");
feed.load(function(result) {
if (!result.error) {
var container = document.getElementById("feed");
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
var entryTitle = entry.title;
var entryContent = entry.content;
imgContent = entryContent + "<p>"+entryTitle+"</p>";
var div = document.createElement("div");
div.className = "image";
div.innerHTML = imgContent;
container.appendChild(div);
}
}
});
}
google.setOnLoadCallback(initialize);
I am loading Google API using google.load()
and I need to process some of what is built by it, but I need to run the JavaScript after it has already completely loaded, is there a way to ensure that happens?
Here is how I build the list of images, I need to add an attribute to each img
tag though, can't do that until after it is built right?
google.load("feeds", "1");
function initialize() {
var feed = new google.feeds.Feed("myfeed.rss");
feed.load(function(result) {
if (!result.error) {
var container = document.getElementById("feed");
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
var entryTitle = entry.title;
var entryContent = entry.content;
imgContent = entryContent + "<p>"+entryTitle+"</p>";
var div = document.createElement("div");
div.className = "image";
div.innerHTML = imgContent;
container.appendChild(div);
}
}
});
}
google.setOnLoadCallback(initialize);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个简单的方法是在 setOnLoadCallback 中创建一个匿名函数,如下所示:
然后只需修改初始化函数即可。
f
函数initialize() {
>functioninitialize(callback){
这样您就不会将匿名函数传递给初始化。然后在
feed.load(function(result) {});
中,您只需执行callback()
即可激活回调。你说匿名函数让你迷惑,我先给你介绍几种写函数的方法
function hello(){}
hello = function(){}
以上是非匿名函数
匿名函数是未分配给变量的函数,因此解析器会遍历该函数并且不会执行它
运行上面的命令将在对话框中显示 Start 和 End,但 Middle 永远不会显示,因为该函数尚未被执行被执行。
执行以下操作:
其中 F 是匿名函数,P 是应该在范围内的参数,您可以直接运行匿名函数,而无需将该函数分配给变量,因此:
所以
document
得到传递到匿名函数并成为该范围内的 Doc,并且 Start、Middle 和 End 都将使用 自执行匿名函数好吧,我的解释可能有点过头了,所以我只是快速向您展示原始方法是如何工作的
http://en.wikipedia.org/wiki/Ajax_(programming)
Ajax 是异步的,这意味着当您调用诸如 google 之类的服务器来获取某些信息,您的 javascript 将继续执行,这意味着在您等待数据时,您的 javscript 已继续执行。
这就是为什么我们像
initialize
回调一样专门使用回调, google Javascript 库在获取数据时执行,在创建我们自己的数据时执行,并在我们想要的时候执行。A simple way to do this is by creating an anonymous function in the
setOnLoadCallback
like so:and then just modify your initialize function.
f
unction initialize() {
>function initialize(callback) {
so that you no are passing in an anonymous function to the intialize.and then within the
feed.load(function(result) {});
you can just docallback()
to activate your callback.you say that anonymous function confuses you, Let me first show you a few ways to write a function
function hello(){}
hello = function(){}
The above are non-anonymous functions
Anonymous functions are functions that are not assigned to a variable, so the parser goes over the function and does not execute it
Running the above will show Start and End in a dialog but Middle will never show as the function has not been executed.
Function the following:
Where F is an anonymous function and P Is the param that should be in the scope, you can directly run anonymous functions without ever assigning to the function to a variable liek so:
So
document
gets passed into the anonymous function and becomes Doc in that scope, and Start, Middle and End all will get executed as your using a self executed anonymous functionOk so I might have gone over the top with the explanation so ill just quickly show you how the original method works
http://en.wikipedia.org/wiki/Ajax_(programming)
Ajax is Asynchronously which means that when your calling a server such as google for some information, your javascript will continue to execute which means while your waiting for data your javscript has carried on executing..
This is why we devoted callbacks just like the
initialize
callback, which the google Javascript library executes when its got the data, when have create our own and were executing when we want to.是的,API 文档指定您可以在第三个参数中包含回调函数
google.load()
方法。像这样的东西应该有效:
Yes, the API documentation specifies that you can include a callback function in the third parameter of the
google.load()
method.Something like this should work: