Problem:
window.setTimeout method doesn't give easy way to pass object type argument to the timeout method. And it may cause memory leak too. To avoid this, I have written a simple script which does the same thing.
Kovil Pillai P.
window.setTimeout method doesn't give easy way to pass object type argument to the timeout method. And it may cause memory leak too. To avoid this, I have written a simple script which does the same thing.
var PKTimer = new function()
{
this.timeoutCnt = 1;
}
PKTimer.setTimeout = function(callFn, thisObj, argArr, timeWait)
{
if(argArr === null)
{
return setTimeout(callFn,timeWait);
}
var uniqueId = 'pkt' + PKTimer.timeoutCnt++;
PKTimer[uniqueId] = [callFn, thisObj, argArr];
return setTimeout("PKTimer.timeoutAction('" + uniqueId + "')", timeWait);
}
PKTimer.timeoutAction = function(timeOutId)
{
var callInfo = PKTimer[timeOutId];
if(!callInfo)
{
return;
}
var callFn = callInfo[0];
var thisObj = callInfo[1];
var callArg = callInfo[2];
if(!(callFn instanceof Function))
{
callFn = thisObj[callFn];
}
callFn.apply(thisObj, callArg);
delete PKTimer[timeOutId];
}
function sum(a, b, c)
{
alert("Sum: " + (a + b + c));
}
function MathClass(a, b, c)
{
this.a = a;
this.b = b;
this.c = c;
this.sum = function()
{
alert("MathSum: " + (this.a + this.b + this.c));
}
}
var mathObj = new MathClass(4, 5, 10);
PKTimer.setTimeout(sum, null, [4, 5, 6], 3000);
PKTimer.setTimeout(mathObj.sum, mathObj, [], 6000);
Kovil Pillai P.
Comments