Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get the instance name of an object
    text
    copied!<p>I use the below code to write code to query a web method in a specified interval. <br /><br /> now in the this.Poll function I have to do</p> <pre><code>this.tmo = setTimeout(this.strInstanceName + ".Poll()", this.iInterval); </code></pre> <p>instead of </p> <pre><code>this.tmo = setTimeout(this.Poll(), this.iInterval); </code></pre> <p>because IE looses the this pointer after setTimeout <br /> So I have to pass the class it's instance name:</p> <pre><code> var objPoll = new cPoll("objPoll"); </code></pre> <p><br /> How can I get the instance name without passing it as parameter ? <br /> I want to have it outta there ! <br /></p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" &gt; &lt;head&gt; &lt;title&gt;Intervall-Test&lt;/title&gt; &lt;script type="text/javascript" language="javascript"&gt; 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 &amp;&amp; iIntervalParameter &gt; 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"); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Test&lt;/h1&gt; &lt;input type="Button" value="Start polling" onclick="objPoll.Start(CallBackFunction,3000);" /&gt; &lt;input type="Button" value="Stop polling" onclick="objPoll.Stop();" /&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload