How to pass an Array object to setInterval function

I want to pass an array of objects to the setTimer function in Javascript.

setTimer("foo(object_array)",1000); 

We get an error in this code.

** Note: ** Sorry! some correction in my question: is this possible in the setInterval () function.

+4
source share
3 answers

Use an anonymous function instead of the string in the first parameter of the setTimeout or setInterval function:

 // assuming that object_array is available on this scope setInterval(function () { foo(object_array); }, 1000); 

Why does it work:

When you define an inner function, it can refer to variables that are present in their outer closing function even after their parent functions are already completed.

This language function is called closures .

If you pass the string as the first argument to these functions, the code will be executed internally using the eval function call, which is not considered good practice .

Eval provides direct access to the JavaScript compiler and executes the code that it passed with the privileges of the caller, also using eval repeatedly / widely (for example, your setInterval function is a good example) will lead to performance problems.

+17
source

I am going to talk about Luke here because he refers to the use case that the CMS (and most of the answers to this question) do not.

If you need to associate your arguments with a function call during a timeout setting, a simple function window will not work:

 echo = function (txt) { console.log(txt); }; val = "immediate A"; echo(val); val = "delayed"; window.setTimeout(function () { echo(val); }, 1000); val = "immediate B"; echo(val); 

Assuming you are using the Firebug console, “immediate A”, “immediate B”, and then “immediate B” after 1 second will be displayed above. To bind a value during a call to setTimeout, use the Luke trap method. The following slightly modifies it to accept arbitrary functions and argument lengths:

 echo = function (txt) { console.log(txt); }; trap = function (fn, args) { return function() { return fn.apply(this, args); }; }; val = "immediate A"; echo(val); val = "delayed"; window.setTimeout( trap(echo, [val]), 1000); val = "immediate B"; echo(val); 

Not sure if there is a way to pass the context of the caller implicitly, but it could be further decrypted to accept the context argument if "this" doesn't get you there.

+2
source

first is 'setTimeout'

second, do not pass the string. The actual solution depends on the rest of the code. The most reliable way would be to capture the scope:

 var obj_array = something; function trap(obj) { function exec() { foo(obj); } return exec; } setTimeout(trap(obj_array), 1000); 
Trap

returns a function in which your array falls into scope. This is a general function, but to be specific to your problem, it can be simplified:

 var obj_array = something; function trap() { function exec() { foo(obj_array); } return exec; } setTimeout(trap(), 1000); 

or even:

 var obj_array = something; function trap() { foo(obj_array); } setTimeout(trap, 1000); 

and finally thicken to:

 var obj_array = something; setTimeout(function() { foo(object_array); }, 1000); 

EDIT: My functions (or at least 1 iteration of them I found in the backup here)

 Function.prototype.createDelegate = function(inst, args) { var me = this; var delegate = function() { me.apply(inst, arguments); } return args ? delegate.createAutoDelegate.apply(delegate,args) : delegate; }; Function.prototype.createAutoDelegate = function() { var args = arguments; var me = this; return function() { me.apply({}, args); } }; 

Given:

 function test(a, b) { alert(a + b); } 

APPLICATION:

 setTimeout(test.createAutoDelegate(1, 2), 1000); 

OR GIVEN:

 var o = { a:1, go : function(b) { alert(b + this.a); }} 

APPLICATION:

 setTimeout(o.go.createDelegate(o,[5]), 1000); //or setTimeout(o.go.createDelegate(o).createAutoDelegate(5), 1000); 
+1
source

All Articles