怎样在自定义对象的方法中使用setTimeout或setInterval方法定时调用该对象的其他方法?

yaray 2007-11-13 12:43:37
怎样在自定义对象的方法中使用setTimeout或setInterval方法定时调用该对象的其他方法?
示例代码如下:

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


<INPUT TYPE="button" NAME="" value="showText" onclick="var o = new ObjectA('sssssssssssssssssss'); o.showText();">
...全文
409 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
yaray 2007-11-15
  • 打赏
  • 举报
回复
再次更正:
梅老大提供的方式是可行的!
只需将setTimeout修改为setInterval即可满足对象内重复执行某个方法。

下次再提问题时,一定给梅老大的分给补上:)
meizz 2007-11-13
  • 打赏
  • 举报
回复
使用变量作用域来解决这个问题:

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()方法
};
}
  • 打赏
  • 举报
回复
不好意思,是我代码粘贴错了,没问题,见笑了
  • 打赏
  • 举报
回复
楼上的当有多个类的实例的时候你的方法不行
chenyang37 2007-11-13
  • 打赏
  • 举报
回复

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

  • 打赏
  • 举报
回复
我的机器上没有ie7所以只在ie6和ff2.0下实验的,没问题.ie7下没反应不知道错哪了,期待高手的解决吧
yaray 2007-11-13
  • 打赏
  • 举报
回复
楼上:将您的代码保存成html文件后,在IE7中访问没有出现任何提示及报错。
  • 打赏
  • 举报
回复
我只实验出了这种笨方法,期待高手的解决

<!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
  • 打赏
  • 举报
回复
楼上:这样只能响应一次,之后就没反应了。
gzdiablo 2007-11-13
  • 打赏
  • 举报
回复
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);
};
}
gzdiablo 2007-11-13
  • 打赏
  • 举报
回复
这种情况只能用个闭包来储存变量

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);
};
}
nicholsky 2007-11-13
  • 打赏
  • 举报
回复
用了setTimeout或者setInterval后,函数内部一切变量都不会被引入,所以那个this也不指向原来的元素了。

学了孟子的方法,可以用setTimeout(this.viewText,5000);但也发现取值为null。还是建议用document.getElementById
meizz 2007-11-13
  • 打赏
  • 举报
回复
<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-13
  • 打赏
  • 举报
回复
纠正:应该是使用 gzdiablo 和 chenyang37 提供的方式,只是把 setTimeout 改成了 setInterval 。

再次感谢大家的积极回复!!!
yaray 2007-11-13
  • 打赏
  • 举报
回复
问题得到解决(IE7下可用),代码如下(使用 chenyang37 提供的方式;梅老大的方式测试未通过):

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


感谢各位的积极回复!!!

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧