So, I am seeing a curious problem. If I have a function
所以,我看到了一个奇怪的问题。如果我有一个功能
// counter wraps around to beginning eventually, omitted for clarity.
var counter;
cycleCharts(chartId) {
// chartId should be undefined when called from setInterval
console.log('chartId: ' + chartId);
if(typeof chartId == 'undefined' || chartId < 0) {
next = counter++;
}
else {
next = chartId;
}
// ... do stuff to display the next chart
}
This function can be called explicitly by user action, in which case chartId
is passed in as an argument, and the selected chart is shown; or it can be in autoplay mode, in which case it's called by a setInterval
which is initialized by the following:
可以通过用户操作显式调用此函数,在这种情况下,chartId作为参数传入,并显示所选的图表;或者它可以处于自动播放模式,在这种情况下,它由setInterval调用,该函数由以下内容初始化:
var cycleId = setInterval(cycleCharts, 10000);
The odd thing is, I'm actually seeing the cycleCharts()
get a chartId
argument even when it's called from setInterval
! The setInterval
doesn't even have any parameters to pass along to the cycleCharts
function, so I'm very baffled as to why chartId
is not undefined when cycleCharts
is called from the setInterval
.
奇怪的是,我实际上看到cycleCharts()得到一个chartId参数,即使它是从setInterval调用的! setInterval甚至没有任何参数传递给cycleCharts函数,所以当我从setInterval调用cycleCharts时,为什么mapId没有未定义,我感到非常困惑。
3 个解决方案
#1
4
setInterval is feeding cycleCharts actual timing data ( so one can work out the actual time it ran and use to produce a less stilted response, mostly practical in animation )
setInterval正在为cycleCharts实际计时数据提供数据(因此可以计算出它运行的实际时间并用于产生较少的高位响应,这在动画中非常实用)
you want
var cycleId = setInterval(function(){ cycleCharts(); }, 10000);
( this behavior may not be standardized, so don't rely on it too heavily )
(这种行为可能不是标准化的,所以不要过分依赖它)
#2
3
It tells you how many milliseconds late the callback is called.
它告诉你调用回调的时间是多少毫秒。
#3
1
var cycleId = setInterval(cycleCharts, 10000, 4242);
var cycleId = setInterval(cycleCharts,10000,4242);
From the third parameter and onwards - they get passed into the function so in my example you send 4242 as the chartId. I know it might not be the answer to the question you posed, but it might the the solution to your problem? I think the value it gets is just random from whatever lies on the stack at the time of passing/calling the method.
从第三个参数开始 - 它们被传递到函数中,因此在我的示例中,您将4242作为chartId发送。我知道这可能不是你提出的问题的答案,但它可能解决你的问题?我认为它获得的值只是随机传递/调用方法时堆栈上的任何内容。
#1
4
setInterval is feeding cycleCharts actual timing data ( so one can work out the actual time it ran and use to produce a less stilted response, mostly practical in animation )
setInterval正在为cycleCharts实际计时数据提供数据(因此可以计算出它运行的实际时间并用于产生较少的高位响应,这在动画中非常实用)
you want
var cycleId = setInterval(function(){ cycleCharts(); }, 10000);
( this behavior may not be standardized, so don't rely on it too heavily )
(这种行为可能不是标准化的,所以不要过分依赖它)
#2
3
It tells you how many milliseconds late the callback is called.
它告诉你调用回调的时间是多少毫秒。
#3
1
var cycleId = setInterval(cycleCharts, 10000, 4242);
var cycleId = setInterval(cycleCharts,10000,4242);
From the third parameter and onwards - they get passed into the function so in my example you send 4242 as the chartId. I know it might not be the answer to the question you posed, but it might the the solution to your problem? I think the value it gets is just random from whatever lies on the stack at the time of passing/calling the method.
从第三个参数开始 - 它们被传递到函数中,因此在我的示例中,您将4242作为chartId发送。我知道这可能不是你提出的问题的答案,但它可能解决你的问题?我认为它获得的值只是随机传递/调用方法时堆栈上的任何内容。