Como obter o nome da instância de um objeto
Eu uso o código abaixo para escrever um código para consultar um método da web em um intervalo especificado.
agora na função this.Poll eu tenho que fazer
this.tmo = setTimeout(this.strInstanceName + ".Poll()", this.iInterval);
ao invés de
this.tmo = setTimeout(this.Poll(), this.iInterval);
porque o IE perde o ponteiro this após setTimeout
Então eu tenho que passar na classe, é o nome da instância:
var objPoll = new cPoll("objPoll");
Como posso obter o nome da instância sem passar como parâmetro?
Eu quero tirá-lo de lá!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Intervall-Test</title>
<script type="text/javascript" language="javascript">
function test()
{
alert("Test");
test.tmo = setTimeout(test, 2000);
test.Clear = function()
{
clearTimeout(test.tmo);
}
}
function cPoll(strInstanceName)
{
this.strInstanceName = strInstanceName ;
this.iInterval = 2000;
this.tmo=null;
this.cbFunction=null;
this.Poll = function()
{
this.cbFunction();
this.tmo = setTimeout(this.strInstanceName + ".Poll()", this.iInterval);
}
this.Start = function(pCallBackFunction, iIntervalParameter)
{
if(this.tmo != null)
this.Stop();
if(iIntervalParameter && iIntervalParameter > 0)
this.iInterval=iIntervalParameter;
this.cbFunction=pCallBackFunction;
if(this.cbFunction!=null)
this.Poll();
else
alert("Invalid or no callback function specified");
}
this.Stop = function()
{
if(this.tmo != null)
{
clearTimeout(this.tmo);
this.tmo=null;
}
}
}
function CallBackFunction()
{
alert("PollCallBack");
}
// test();
// test.Clear();
var objPoll = new cPoll("objPoll");
</script>
</head>
<body>
<h1>Test</h1>
<input type="Button" value="Start polling" onclick="objPoll.Start(CallBackFunction,3000);" />
<input type="Button" value="Stop polling" onclick="objPoll.Stop();" />
</body>
</html>