如何获取对象的实例名称 [英] How to get the instance name of an object

查看:157
本文介绍了如何获取对象的实例名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码来编写代码来在指定的时间间隔内查询web方法。



现在在this.Poll函数中我必须做

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);

而不是

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

因为IE在setTimeout后丢失了this指针


我必须传递类的实例名称:

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>


推荐答案

我问过另一个问题, :

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

JavaScript:在IE中列出全局变量

遍历全局变量并检查它是否等于this。

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

这篇关于如何获取对象的实例名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆