首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 怎样在自定义对象的方法中使用setTimeout或setInterval方法定时调用该对象的其他方法? [已结贴,结贴人:yaray]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • yaray
    • 等级:
    发表于:2007-11-13 00:43:37 楼主
    怎样在自定义对象的方法中使用setTimeout或setInterval方法定时调用该对象的其他方法?
    示例代码如下:
    JScript code
    function ObjectA(text) { this.text = (text!=null) ? text : "not set the text."; this.viewText = function() { alert(this.text); }; this.showText = function() { setTimeout("this.viewText()", 5000);//怎样定时调用对象内的viewText()方法 //setInterval("this.viewText()", 5000); }; }

    HTML code
    <INPUT TYPE="button" NAME="" value="showText" onclick="var o = new ObjectA('sssssssssssssssssss'); o.showText();">
    20  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-13 02:45:351楼 得分:0
    用了setTimeout或者setInterval后,函数内部一切变量都不会被引入,所以那个this也不指向原来的元素了。

    学了孟子的方法,可以用setTimeout(this.viewText,5000);但也发现取值为null。还是建议用document.getElementById
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-13 08:47:072楼 得分:0
    这种情况只能用个闭包来储存变量

    JScript code
    function ObjectA(text) { var This = this; this.text = (text!=null) ? text : "not set the text."; this.viewText = function() { alert(This.text); }; this.showText = function() { setTimeout(this.viewText(), 5000); //setInterval("this.viewText()", 5000); }; }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-13 08:48:033楼 得分:10
    JScript code
    function ObjectA(text) { var This = this; this.text = (text!=null) ? text : "not set the text."; this.viewText = function() { alert(This.text); }; this.showText = function() { setTimeout(this.viewText, 5000);//viewText是语句柄不能加() //setInterval("this.viewText()", 5000); }; }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • yaray
    • 等级:
    发表于:2007-11-13 08:52:314楼 得分:0
    楼上:这样只能响应一次,之后就没反应了。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-13 09:24:565楼 得分:0
    我只实验出了这种笨方法,期待高手的解决

    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档 </title>
    <script language="javascript">
    Array.prototype.isIn=function(obj){
    for(var i in this)
    if(this[i]===obj)
    return i;
    return -1;
    }

    window.temp=[];

    function ObjectA(text) {
        this.text = (text!=null) ? text : "not set the text.";

        this.viewText = function() {
            alert(this.text);
        };

        this.showText = function() {
    var nIndex=window.temp.isIn(this);
    alert(nIndex);
    if(nIndex==-1){
    window.temp.push(this);
    nIndex=window.temp.length-1;
    }
            setInterval("(function(){window.temp["+nIndex+"].viewText();})()", 2000);//怎样定时调用对象内的viewText()方法
        };
    }

    var obj1=new ObjectA("abc");
    var obj2=new ObjectA("efg");
    obj1.showText();
    obj2.showText();
    </script>
    </head>

    <body>
    </body>
    </html>
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • yaray
    • 等级:
    发表于:2007-11-13 10:11:106楼 得分:0
    楼上:将您的代码保存成html文件后,在IE7中访问没有出现任何提示及报错。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-13 10:33:007楼 得分:0
    我的机器上没有ie7所以只在ie6和ff2.0下实验的,没问题.ie7下没反应不知道错哪了,期待高手的解决吧
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-13 11:07:088楼 得分:10
    HTML code
    <SCRIPT LANGUAGE="JavaScript"> <!-- function ObjectA(text) { this.property = "o"; var s_obj = this; this.text = (text!=null) ? text : "not set the text."; this.viewText = function() { alert(s_obj.text); }; this.showText = function() { setTimeout(s_obj.viewText, 1000);//怎样定时调用对象内的viewText()方法 //setInterval("this.viewText()", 5000); }; } //--> </SCRIPT> <INPUT TYPE="button" NAME="" value="showText" onclick="var o = new ObjectA('sssssssssssssssssss'); o.showText();">

    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-13 12:00:079楼 得分:0
    楼上的当有多个类的实例的时候你的方法不行
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-13 12:01:5510楼 得分:0
    不好意思,是我代码粘贴错了,没问题,见笑了
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-13 12:06:5111楼 得分:0
    使用变量作用域来解决这个问题:

    HTML code
    function ObjectA(text) { this.text = (text!=null) ? text : "not set the text."; this.viewText = function() { alert(this.text); }; this.showText = function() { var me = this; setTimeout(function(){me.viewText();}, 5000); //怎样定时调用对象内的viewText()方法 }; }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • yaray
    • 等级:
    发表于:2007-11-13 13:35:0212楼 得分:0
    问题得到解决(IE7下可用),代码如下(使用 chenyang37 提供的方式;梅老大的方式测试未通过):
    HTML code
    <SCRIPT LANGUAGE="JavaScript"> <!-- function ObjectA(text) { this.text = (text!=null) ? text : "not set the text."; var instanceObjectA = this;//对象实例 var timer = null;//控制时钟(只能声明为变量,不能声明为对象级变量(即: this.timer = null;),否则此时钟不能被清除) var times = 5;//重复次数 // 在被时钟重复调用的方法中,引用对象级变量(即:this.xxx)只能使用 instanceObjectA.xxxx 来访问 this.viewText = function() { if(times==0) { clearInterval(timer);//清除控制时钟 timer = null; alert(instanceObjectA.text + " clear timer : down"); return; } alert(instanceObjectA.text + " : " + times); times--; }; this.showText = function() { timer = setInterval(this.viewText, 3000); }; } //--> </SCRIPT>

    感谢各位的积极回复!!!
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • yaray
    • 等级:
    发表于:2007-11-13 13:40:4513楼 得分:0
    纠正:应该是使用 gzdiablo 和 chenyang37 提供的方式,只是把 setTimeout 改成了 setInterval 。

    再次感谢大家的积极回复!!!
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-13 14:20:1314楼 得分:0
    JScript code
    <script language="JavaScript"> function ObjectA(text) { this.text = (text!=null) ? text : "not set the text."; this.viewText = function() { alert(this.text); }; this.showText = function() { var me = this; setTimeout(function(){me.viewText();}, 5000); // }; } var e = new ObjectA(); //需要用 new 初始化 e.showText() </script>
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • yaray
    • 等级:
    发表于:2007-11-15 20:13:3515楼 得分:0
    再次更正:
        梅老大提供的方式是可行的!
        只需将setTimeout修改为setInterval即可满足对象内重复执行某个方法。

        下次再提问题时,一定给梅老大的分给补上:)
    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    世纪乐知(北京)网络技术有限公司 版权所有 京 ICP 证 020026 号
    Copyright © 2000-2007, CSDN.NET, All Rights Reserved