If your dothis
function has no other need for a return value, you can have it return itself.
This will allow you to invoke and pass it at the same time. If the return value is otherwise ignored, it will be harmless.
function dothis() {
// your code
return dothis;
}
var i = setInterval(dothis(), 20000);
Otherwise, you could extend Function.prototype
to give an invoke and return functionality to all your functions:
DEMO: http://jsfiddle.net/ZXeUz/
Function.prototype.invoke_assign = function() {
var func = this,
args = arguments;
func.call.apply( func, arguments );
return function() { func.call.apply( func, args ); };
};
setInterval( dothis.invoke_assign( 'thisArg', 1, 2, 3 ), 20000 );
// thisArg 1 2 3
// thisArg 1 2 3
// thisArg 1 2 3
// ...
This actually enhances things a bit. It lets you pass a set of arguments. The first argument will set the this
value of the function you're invoking, and the rest of the arguments will be passed on as the regular arguments.
Because the function returned is wrapped in another function, you'll have identical behavior between the initial and interval invocations.