如何使用.execute时传递额外的参数给回调JavaScript函数()? [英] How to pass additional arguments to callback functions in javascript when using .execute()?
问题描述
我和JavaScript来谷歌Analytics API的工作,但我并不比JS新手多。我有C ++和Java知识,我可以用逻辑思维相处,但有些事情困扰我。在GA API我可以做一个函数调用是这样的:
I'm working with javascript for Google Analytics API but I'm no more than a novice in JS. I have C++ and Java knowledge and I can get along with logical thinking but some things puzzle me. In GA API I get to make a function call like this:
gapi.client.analytics.data.ga.get({'ids':'<tableID>',
'start-date':'<startDate>',
'end-date':'<endDate>',
'metrics':'<metrics>',
'filters':'<filters>',
'samplingLevel':'HIGHER_PRECISION',}).execute(putToVar);
putToVar()
像这样定义一个用户定义的函数:
putToVar()
is a user defined function defined like so:
function putToVar(results)
{
//do processing with the results
}
这是我的理解是, .execute()
方法用于调用回调函数异步调用 gapi.client.analytics。 data.ga.get()
。所以我想什么功能1()执行(函数2)
做的是调用函数2
从返回值功能1
作为参数?这是正确的?
It is my understanding that the .execute()
method is used to invoke a callback function for the asynchronous call gapi.client.analytics.data.ga.get()
. So I assume what function1().execute(function2)
does is call function2
with the return value from function1
as argument? Is this correct?
我在一个情况下,我需要应用几个不同的过滤器,并将其存储在一个数组作为需要时进行检索,而不论该API调用是否返回一个结果对象(这是一个异步调用,所以我不知道当响应到来的时候,它是唯一可见的回调函数)。
I'm in a situation where I need to apply several distinct filters and store them in an array to retrieve as and when required, irrespective of whether the API call returned a results object (it is an async call, so I don't know when the response comes, it is only visible to the callback function).
我想传递给回调函数数组中来存储返回的对象,这样我可以按需以后检索它们的尺寸,而不必担心其中的响应得到处理的顺序。我这样说是因为,最初我尝试了为
循环,我在其中得到了回应我的API调用的顺序不一样,我在其中放置API调用的顺序我的查询,所以有不匹配。
I would like to pass to the callback function the dimensions of the array in which to store the returned objects so that I can retrieve them on demand later, without worrying about the order in which the responses get processed. I say this because, initially I tried a for
loop and the order in which I got the response to my API calls were not the same as the order in which I placed API calls for my queries, so there were mismatches.
由于参考使用这个方法来调用回调函数,我想知道如何使用时,通过附加参数像这样一个回调函数 .execute()
方法,当我写 putToVar()
函数是这样的:
Since the reference uses this method to invoke the callback function, I would like to know how to pass additional arguments to a callback function like this when using .execute()
method, when I get to write putToVar()
function something like this:
function putToVar(results,arrayDim)
{
//Process Results
//Store in Array[arrayDim] the required value
}
我希望我已经说清楚了。
我已阅读下列职位
I hope I have made myself clear. I have read the following posts
- How do I pass multiple arguments into a javascript callback function?
- passing arguments to callback
- How to explain callbacks in plain english? How are they different from calling one function from another function?
- How to pass additional arguments to callbacks and also access default parameters?
但他们都不使用 .execute()
方法,我无法弄清楚如何使用他们的说法。或者说,是否以及如何我 .execute()
方法(回调执行型)可以进行修改,以帮助我的目的。
but none of them seem to use the .execute()
method and I cannot figure out how to use what they have said. Or, if and how my .execute()
method (type of callback execution) can be modified to help my purpose.
推荐答案
添加关闭将解决您的问题:
Adding a closure would solve your problem:
for(var x = 0; x< n x ++){ //Or any other loop
(function(x){ //Closure. This line does the magic!!
var arrayDim = something_that_depends_on_x_or_the_loop,
param2 = some_other_thing_that_depends_on_x;
gapi.client.analytics.data.ga.get({'ids':'<tableID>',
'start-date':'<startDate>',
...
}).execute(function putToVar(results){ //this fn is defined inline to get access to param1, param2
//The following alerts will use the *correct* variables thanks to the closure
alert(x);
alert(arrayDim);
alert(param2);
});
})(x); //This one too
}
闭合确实神奇。这将允许每个循环有它自己的变量(不共享),所以适当的人会在 putToVar
时执行。
我希望这是明显的,如果没有,只是让我知道。
I hope it's clear, if not, just let me know.
只是测试一下吧!
欢呼声中,来自拉巴斯,玻利维亚
Cheers, from La Paz, Bolivia
这篇关于如何使用.execute时传递额外的参数给回调JavaScript函数()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!