我可以在Google Loader运行后运行JavaScript函数吗? [英] Can I run a JavaScript function AFTER Google Loader has run?

查看:80
本文介绍了我可以在Google Loader运行后运行JavaScript函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 google.load()加载Google API,并且需要处理其构建的部分内容,但是需要在JavaScript之后运行JavaScript已经完全加载,有没有办法确保发生?



下面是我如何构建图像列表,我需要为每个 img 标签添加一个属性,不能这样做,直到它被建立好后?

  google.load(feeds,1); 

函数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);


解决方案

一个简单的方法是创建一个匿名函数在 setOnLoadCallback 像这样:

  google.setOnLoadCallback(function ){

//运行初始化函数
initialize(function(){
//在这里运行其他任何东西,如
alert(我的函数可以在这里运行: ))
});
});

然后修改您的初始化函数。



< ()函数初始化(回调){,以便你不传入匿名函数到intialize。



然后在 feed.load(function(result){}); 你可以只用 callback()来激活你的回调。






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


  • function hello (){}

  • hello = function(){}


以上是非匿名函数



匿名函数是未分配给变量的函数,所以解析器检查函数并且不执行它

$ p $ alert(Start)
function()
{
alert(Middle)
}
alert(End);

运行上述操作会显示对话框中的开始和结束,但中间不会显示,因为函数没有



函数如下:

pre $ P)

其中F是一个匿名函数,P是应该在范围内的参数,可以直接运行匿名函数,而不必将函数分配给变量liek,如下所示:

  alert(Start)

函数(Doc){
alert(Middle);
}
)(文档)
alert(End)

所以文档被传入匿名函数并成为Doc范围和开始,中间和结束全部将被执行,因为您使用自我执行的匿名函数



好吧,我可能已经过了t他顶部的解释如此恶心,只是很快就会告诉你原来的方法是如何工作的

http://en.wikipedia.org/wiki/Ajax_(programming)

Ajax是异步的,它意味着当你调用服务器如谷歌的一些信息,你的JavaScript将继续执行,这意味着当你等待你的javscript进行执行数据。



这就是为什么我们会像回调初始化回调一样使用回调函数,当JavaScript数据库获取数据时执行回调函数,创建我们自己的函数并在需要时执行。 / p>

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);

解决方案

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.

这篇关于我可以在Google Loader运行后运行JavaScript函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆