关于多页面调用时钟显示功能的问题 本帖最后由 ldwd 于 2011-12-27 16:44:35 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 this.showtip = showtip; //将属性赋值。这里的this.showtip只属于第一次进入的show函数空间,即使你后面把他传递给了setTimeout中的第二个show函数,但只是传了个变量的指针,等setTimeout执行第二个show函数的时候,第一个show函数的空间已经消失,它的this.showtip变量也消失,既然这个this.showtip指针没有指向任何东西,当然就变成null了 this.showtip = showtip; //将属性赋值。这里的this.showtip只属于第一次进入的show函数空间,即使你后面把他传递给了setTimeout中的第二个show函数,但只是传了个变量的指针,等setTimeout执行第二个show函数的时候,第一个show函数的空间已经消失,它的this.showtip变量也消失,既然这个this.showtip指针没有指向任何东西,当然就变成null了 那要如何修改这段代码呢?是不是setTimeout函数没法在封装的类中使用? var obj = this;this.show = function( ) //定时循环的函数 { alert(typeof obj.showtip); if(obj.timerRunning) clearTimeout(obj.timerID); obj.timerRunning = false var now = new Date(); var hours = now.getHours(); var minutes = now.getMinutes(); var seconds = now.getSeconds(); var timeValue = "" + ((hours < 10) ? "0" : "") + hours; timeValue += ((minutes < 10) ? ":0" : ":") + minutes timeValue += ((seconds < 10) ? ":0" : ":") + seconds; if(showtip != null) { obj.showtip.innerHTML = timeValue; } obj.timerID = setTimeout("show()" ,1000); obj.timerRunning = true; } 我找到了解决方法,方法如下即可:this.showtip = null; this.timerID = null; this.timerRunning = false; // Make sure the clock is stopped this.show = function() { var obj = this; //alert(typeof obj.showtip); if(obj.timerRunning) clearTimeout(obj.timerID); obj.timerRunning = false; var now = new Date(); var hours = now.getHours(); var minutes = now.getMinutes(); var seconds = now.getSeconds(); var timeValue = "" + ((hours < 10) ? "0" : "") + hours; timeValue += ((minutes < 10) ? ":0" : ":") + minutes timeValue += ((seconds < 10) ? ":0" : ":") + seconds; if(obj.showtip != null) obj.showtip.innerHTML = timeValue; obj.timerID = setTimeout(function(){obj.show();},1000); obj.timerRunning = true; }把四楼的var obj = this移到函数内,并将setTimeout第一个参数改为函数,即可完美解决。 javascript如何取数组中时间计算,算出分钟 Google Chrome浏览器js相对路径的问题 Flex3中的ConvolutionFilter类,参数个数问题!!! js计算错误问题? JS正则问题,帮帮忙 JS如何同步引用JS 使用对话框输入一串文字,就会在网页的某个预先定义的部分显示出来。请问这个功能如何实现? 判断文件大小 如何对网页源代码进行Encode编码呢!高手请指点~~~ 如何得到"<span id=name>3</span>"3这个值?document.all("name").????? URL链接解码问题 js的原型继承问题,有些不解...
这里的this.showtip只属于第一次进入的show函数空间,即使你后面把他传递给了setTimeout中的第二个show函数,但只是传了个变量的指针,等setTimeout执行第二个show函数的时候,第一个show函数的空间已经消失,它的this.showtip变量也消失,既然这个this.showtip指针没有指向任何东西,当然就变成null了
这里的this.showtip只属于第一次进入的show函数空间,即使你后面把他传递给了setTimeout中的第二个show函数,但只是传了个变量的指针,等setTimeout执行第二个show函数的时候,第一个show函数的空间已经消失,它的this.showtip变量也消失,既然这个this.showtip指针没有指向任何东西,当然就变成null了
this.show = function( ) //定时循环的函数
{
alert(typeof obj.showtip);
if(obj.timerRunning)
clearTimeout(obj.timerID);
obj.timerRunning = false
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var timeValue = "" + ((hours < 10) ? "0" : "") + hours;
timeValue += ((minutes < 10) ? ":0" : ":") + minutes
timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
if(showtip != null)
{
obj.showtip.innerHTML = timeValue;
}
obj.timerID = setTimeout("show()" ,1000);
obj.timerRunning = true;
}
this.timerID = null;
this.timerRunning = false;
// Make sure the clock is stopped
this.show = function()
{
var obj = this;
//alert(typeof obj.showtip);
if(obj.timerRunning)
clearTimeout(obj.timerID);
obj.timerRunning = false;
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var timeValue = "" + ((hours < 10) ? "0" : "") + hours;
timeValue += ((minutes < 10) ? ":0" : ":") + minutes
timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
if(obj.showtip != null)
obj.showtip.innerHTML = timeValue;
obj.timerID = setTimeout(function(){obj.show();},1000);
obj.timerRunning = true;
}
把四楼的var obj = this移到函数内,并将setTimeout第一个参数改为函数,即可完美解决。