dothis
関数の戻り値が他に必要ない場合は、それ自身を返すことができます。
これにより、同時に呼び出して渡すことができます。戻り値が無視されると、無害になります。
function dothis() {
// your code
return dothis;
}
var i = setInterval(dothis(), 20000);
そうしないと、呼び出しを与えると、すべての機能に機能を返すようにFunction.prototype
を拡張できます。
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
// ...
これは実際に物事を少し高め。それは引数のセットを渡すことができます。最初の引数は呼び出す関数のの値を設定し、残りの引数は正規の引数として渡されます。
返される関数は別の関数でラップされるため、初期呼び出しとインターバル呼び出しの動作が同じになります。
お役立ち情報ありがとう! – Andrew