我可以在 Google Loader 运行后运行 JavaScript 函数吗?

发布于 2024-10-09 18:11:15 字数 1032 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弄潮 2024-10-16 18:11:15

一个简单的方法是在 setOnLoadCallback 中创建一个匿名函数,如下所示:

google.setOnLoadCallback(function(){

    //Run the Initialize Function
    initialize(function(){
         //Run anything else here like
         alert("My function can run here :)")  
    });
});

然后只需修改初始化函数即可。

f函数initialize() { > functioninitialize(callback){这样您就不会将匿名函数传递给初始化。

然后在 feed.load(function(result) {}); 中,您只需执行 callback() 即可激活回调。


你说匿名函数让你迷惑,我先给你介绍几种写函数的方法

  • function hello(){}
  • hello = function(){}

以上是非匿名函数

匿名函数是未分配给变量的函数,因此解析器会遍历该函数并且不会执行它

alert("Start")
function()
{
    alert("Middle")
}
alert("End");

运行上面的命令将在对话框中显示 Start 和 End,但 Middle 永远不会显示,因为该函数尚未被执行被执行。

执行以下操作:

( F )( P )

其中 F 是匿名函数,P 是应该在范围内的参数,您可以直接运行匿名函数,而无需将该函数分配给变量,因此:

alert("Start")
(
    function(Doc){
        alert("Middle");
    }
)(document)
alert("End")

所以 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 setOnLoadCallbacklike so:

google.setOnLoadCallback(function(){

    //Run the Initialize Function
    initialize(function(){
         //Run anything else here like
         alert("My function can run here :)")  
    });
});

and then just modify your initialize function.

function 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 do callback() 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

alert("Start")
function()
{
    alert("Middle")
}
alert("End");

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:

( F )( P )

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:

alert("Start")
(
    function(Doc){
        alert("Middle");
    }
)(document)
alert("End")

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 function

Ok 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.

冷月断魂刀 2024-10-16 18:11:15

是的,API 文档指定您可以在第三个参数中包含回调函数google.load() 方法。

像这样的东西应该有效:

google.load("jquery", "1.4.2", {
    callback: function() {
        alert('Finished!');
    }
});

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:

google.load("jquery", "1.4.2", {
    callback: function() {
        alert('Finished!');
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文