87,907
社区成员
发帖
与我相关
我的任务
分享
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();">
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()方法
};
}
<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();">
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);
};
}
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);
};
}
<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>
<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>