Javascript更新';全球';函数中具有值的数组
昨天,我需要帮助&被介绍到闭包(我仍在努力解决这个问题) 在函数开始时,我创建了数组,需要使用在子函数中创建的值填充数组。我知道“return”只是返回值&神奇地更新数组,显然不是这样。我也试过推,但还是没有魔法Javascript更新';全球';函数中具有值的数组,javascript,arrays,variables,scope,Javascript,Arrays,Variables,Scope,昨天,我需要帮助&被介绍到闭包(我仍在努力解决这个问题) 在函数开始时,我创建了数组,需要使用在子函数中创建的值填充数组。我知道“return”只是返回值&神奇地更新数组,显然不是这样。我也试过推,但还是没有魔法 eEndDesigning.onclick = publicity; function publicity() { var eCamerasArray = $(".cCameras").toArray(); // this is the array that nee
eEndDesigning.onclick = publicity;
function publicity() {
var eCamerasArray = $(".cCameras").toArray();
// this is the array that needs to be updated with the annon. func. inside setTimeout func.
var iLeftPosArray = [];
// this is the second array that needs to be updated
var iTopPosArray = [];
for (var i = 0; i < eCamerasArray.length; i++) {
var timer = Math.floor(Math.random() * 300) + 100;
(function(i) // a closure function
{
window.setTimeout(function() {
// following two values are the ones that need to be fed back to 'global' arrays
iLeftPosArray[i] = Math.floor(Math.random() * 139) + 360;
iTopPosArray[i] = Math.floor(Math.random() * 160) + 100;
$(eCamerasArray[i]).animate({
left: iLeftPosArray[i] + "px",
top: iTopPosArray[i] + "px"
}, 100, "linear");
return [iLeftPosArray[i], iTopPosArray[i]];
}, timer);
}(i));
}
}
eendesigning.onclick=宣传;
职能宣传(){
var eCamerasArray=$(“.cCameras”).toArray();
//这是需要使用annon.func.inside setTimeout func更新的数组。
var iLeftPosArray=[];
//这是需要更新的第二个阵列
变量数组=[];
对于(变量i=0;i
您可能希望将数组iLeftPosArray
和ITOPOSARRAY
向上移动一级,因为它们当前不是全局数组,但具有功能范围 你到底想达到什么目的?你需要暂停是有原因的吗?很抱歉打破了魔咒,但是没有什么神奇的东西:-)你到底想做什么?您的问题不清楚。请重新设置超时,是的,以便obj。在指定的限制内以随机间隔设置动画@KarlMendes,是的,我想重用已经计算的值(iLeftPosArray[I]),在原始对象的特定距离内为其他对象设置动画。从任何类型的事件处理程序(包括计时器处理程序,如您的情况)返回值只与浏览器的事件调度机制通信。因此,返回应用程序数据是毫无意义的。我一直在努力保持聪明,在函数中保留“iLeftPosArray”的范围,因为这是它唯一需要的。