I read an article about how speed up javascript, I try copy it's code which could improve loop speed:
var chunk = function (array, process, context) {
setTimeout(function(){
var item = array.shift();
console.log('item', item);//this could show correctly
process.call(item, context);
if (array.length > 0){
setTimeout(arguments.callee, 100);
}
}, 100);
}
Then I try to pass my parameter into it, but I don't know how to use the context parameter, what I have done is:
var dosomething1 = function (item) {
console.log('this is begin ' + item)
}
var dosomething2 = function (item) {
console.log('this is end ' + item);
}
var process = function (item) {
console.log(item); //this show undefined
dosomething1(item);
dosomething2(item);
}
var temp = ["a", "b", "c", "d"];
chunk(temp, process);
The problem is begin in the process function, the item log undefined, the item could only show correctly in chunk.
So how can I solve this problem?I think it related to the process.call method?Is it related to the context parameter?
You can see the demo here