我一直在嘗試兩天來將數組傳遞給setTimeout回調函數。將數組作爲參數傳遞給SetTimeout回調
我一直在尋找遍及互聯網,並且我已經閱讀了10個不同的StackOverflow問題及其所有答案。我必須錯過一些東西,因爲在嘗試所有這些不同的事情之後,它仍然不起作用。這裏是我的立場現在:
function testing(pixels){
return function(){
for(i=0; i<pixels.length;i++){
a = pixels[i][0];
b = pixels[i][1];
c = pixels[i][2];
d = pixels[i][3];
box = pixels[i][5];
done = pixels[i][6];
color_to_draw = done ? box.color:active_color;
ctx.fillRect(a,b,c,d);
ctx2.clearRect(box.x-1,box.y-1,box.w,box.h);
draw_colored_box(box.x, box.y, box.w, box.h, color_to_draw, box.alpha, true, ctx2);
}
};
}
function ias(pixel_batch){
var color_to_draw;
ctx.fillStyle = "#000000";
var a, b, c, d, e, box, done, i;
setTimeout(testing(pixel_batch),pixel_batch[0][4]);
}
我已經得到了所有不同的解決方案,我發現那應該這裏我的方法工作。我顯然做錯了,因爲它不起作用。
的問題是,在功能ias()
,pixel_batch.length
等於3或然而,許多項目將投入到該數組,即使在功能testing()
,pixels.length
是正確的值,但通過測試返回的功能裏面, pixels.length`等於0 ...
原來,這就是我曾試圖:
function ias(pixel_batch){
var color_to_draw;
ctx.fillStyle = "#000000";
var a, b, c, d, e, box, done, i;
setTimeout((function(pixels){
console.log(pixels.length);
return function(){
for(i=0; i<pixels.length;i++){
a = pixels[i][0];
b = pixels[i][1];
c = pixels[i][2];
d = pixels[i][3];
box = pixels[i][5];
done = pixels[i][6];
color_to_draw = done ? box.color:active_color;
ctx.fillRect(a,b,c,d);
ctx2.clearRect(box.x-1,box.y-1,box.w,box.h);
draw_colored_box(box.x, box.y, box.w, box.h, color_to_draw, box.alpha, true, ctx2);
}
};
})(pixel_batch),pixel_batch[0][4]);
}
由於相信它並不需要通過外部定義的函數來完成,但在這一點我已經開始嘗試任何事情/一切。
如何將pixel_batch(將參數傳遞給ias()
)轉換爲setTimeout的回調函數?
[編輯/ UPDATE] 下面是實際調用ias()
代碼:
function redraw_boxes(){
//This loop simply draws the active boxes again, on top of the previous set.
//At this point in time there is no need to clear the canvas at all.
var i; var i2; var box;
var temp_pixelation_array = pixelation_array.slice(0);
var x_mod; var y_mod;
var random_array_key;
var max_runs;
var the_pixel_batch = [];
var num_pixels_per_batch = 3;
var speed_to_pixelate = 3;
var done;
var temptimer=0;
var timers = [];
for(i=0;i<newly_acquired_boxes.length;i++){
temptimer=0;
temp_pixelation_array = pixelation_array.slice(0);
max_runs = temp_pixelation_array.length;
box = boxes[newly_acquired_boxes[i].column][newly_acquired_boxes[i].row];
for(i2 = 0; i2<max_runs;i2++){
random_array_key = ~~((Math.random()*temp_pixelation_array.length));
x_mod = temp_pixelation_array[random_array_key][0];
y_mod = temp_pixelation_array[random_array_key][1];
temp_pixelation_array.splice(random_array_key,1);
done = i2<max_runs-1 ? true:true ;
the_pixel_batch.push([box.x+x_mod, box.y+y_mod, particle_size, particle_size,temptimer,box,done]);
if(the_pixel_batch.length>= num_pixels_per_batch){
ias(the_pixel_batch);
the_pixel_batch.length = 0;
temptimer += num_pixels_per_batch*speed_to_pixelate;
}
}
}
newly_acquired_boxes.length=0;
}
[2編輯/ UPDATE 2]
我希望我能接受你的答案,因爲你們在技術上都是對的。這是我的錯,因爲沒有給你正確的信息開始。我向每個人投了票,因爲你們都應該得到答案,你無法用所提供的信息給我。
在一個非常快的第一眼,你的方法似乎聽起來至少有 –
我認爲我們需要查看一些數據以便測試。我真的不明白你爲什麼要調用一個首先返回函數的函數。在'ias'中只有一個'setTimeout'被調用,所以回調可以獨佔訪問該函數調用的變量值。 – RightSaidFred
分解你的功能到它的基本部分,它似乎工作:http://jsfiddle.net/pxaXq/ – gilly3