2

I use the below code to write code to query a web method in a specified interval.

now in the this.Poll function I have to do

this.tmo = setTimeout(this.strInstanceName + ".Poll()", this.iInterval);

instead of

this.tmo = setTimeout(this.Poll(), this.iInterval);

because IE looses the this pointer after setTimeout
So I have to pass the class it's instance name:

    var objPoll = new cPoll("objPoll");


How can I get the instance name without passing it as parameter ?
I want to have it outta there !

<!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>
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442

7 Answers7

2

Loose the parenthesis in this.Poll(). You call this function right away, not after a time interval. If you loose the brackets, it will pass a function, not a result of it, to setInterval and you won't have any issues.

setTimeout(this.Poll, this.Interval);

Otherwise you call the function right away and nothing holds this pointer anymore, and IE just deletes it.

In fixed variant, this.Poll will hold pointer to this and it won't be deleted.

vava
  • 24,851
  • 11
  • 64
  • 79
  • Disaster strikes even without braces. Do you use IE != 8 ? – Stefan Steiger Mar 17 '10 at 09:46
  • Do that trick with `self` J-P has posted then. If that doesn't work then your object is being GC'd (or haven't been created) before you call `Start`. So, make sure objPoll is there before you call `Start()` and try to loose `var` before `objPoll = new cPoll("objPoll");` – vava Mar 17 '10 at 10:32
  • GC is possible, but it is there when I click Start, because Start is called from the instance. If it wouldn't exist, it wouldn't start. – Stefan Steiger Mar 17 '10 at 11:21
1

I just wanted to say, this self trick has saved me a ton of hair pulling.

I didn't think to create a reference like this. It means dynamically-created elements with on click events can call the correct instance of my class.

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
user362720
  • 11
  • 1
1

You could also use:

i.e.

var name = findInstanceOf(cPoll);

function findInstanceOf(obj) {
    for (var v in window) {
        try {
            if (window[v] instanceof obj)
                return v;
        } catch(e) { }
    };
    return false;
}

From http://www.liam-galvin.co.uk/2010/11/24/javascript-find-instance-name-of-an-object/#read

Diego Pino
  • 11,278
  • 1
  • 55
  • 57
Liam Galvin
  • 706
  • 3
  • 12
0
var self = this;
this.tmo = setTimeout(function(){
     self.Poll();
}, this.iInterval);
James
  • 109,676
  • 31
  • 162
  • 175
0

I provided an answer for a similar question today in which I created a polling class from scratch. You may want to adopt it for yourself. In the sake of not duplicating, here's a link a link to said question:

Poll the Server with Ajax and Dojo *

* Despite the title, my solution offers both "vanilla" and Dojo styles.

Community
  • 1
  • 1
Justin Johnson
  • 30,978
  • 7
  • 65
  • 89
0

You can use this handy little wrapper to execute a function periodically at given interval (1 sec by default):

function tick(func, interval) {
    return (function() {
        if(func())
            setTimeout(arguments.callee, interval || 1000);
    })();
}

The function is repeated until it returns false:

tick(function() {
   if(should stop) return false;
   do stuff
   return true;
});

If the function is a method, make a closure as shown

// inside your object
var me = this;
tick(function() {
   return me.doStuff();
});
user187291
  • 53,363
  • 19
  • 95
  • 127
0

I've asked another question, the answer is here:

JavaScript: List global variables in IE

Iterating through the global variables and check whether it equals "this".

Community
  • 1
  • 1
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442