[英]Countdown clock timer in Javascript for Flash Canvas HTML5
在JavaScript(Flash)Canvas HTML5中對JavaScript(createJS)進行簡單倒計時的過程中,出現以下錯誤: file_Canvas.js:318 Uncaught TypeError: Cannot read property 'dtxt_days' of undefined
。 (該錯誤特定this.mc_counter.dtxt_days.text = days;
行this.mc_counter.dtxt_days.text = days;
其下的3。
我究竟做錯了什么?
這是js:
this.mc_counter.dtxt_days.text = "";
this.mc_counter.dtxt_hours.text = "";
this.mc_counter.dtxt_mins.text = "";
this.mc_counter.dtxt_secs.text = "";
var end = new Date('10/19/2015 10:1 AM');
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;
function showRemaining() {
var now = new Date();
var distance = end - now;
this.days = Math.floor(distance / _day);
this.hours = Math.floor((distance % _day) / _hour);
this.minutes = Math.floor((distance % _hour) / _minute);
this.seconds = Math.floor((distance % _minute) / _second);
this.mc_counter.dtxt_days.text = days;
this.mc_counter.dtxt_hours.text = hours;
this.mc_counter.dtxt_mins.text = minutes;
this.mc_counter.dtxt_secs.text = seconds;
}
timer = setInterval(showRemaining, 1000);
console.log(timer);
您正在將代碼行中的有錯誤的變量分配給不存在的對象。
this.days != days
相反,請確保您獲取剛剛分配給this
的屬性。
this.mc_counter.dtxt_days.text = this.days;
this.mc_counter.dtxt_hours.text = this.hours;
this.mc_counter.dtxt_mins.text = this.minutes;
this.mc_counter.dtxt_secs.text = this.seconds;
此外, this
與您的想法無關。 由於showRemaining
是一個函數, this
它里面的代碼實際上指的是showRemaining
。 有兩種解決方法。 可以說這是最簡單的一個:
var self = this;
function showRemaining() {
self.days = ...
self.mc_counter.dtxt_days.txt = ...
}
那只是存儲對外部this
的引用。 或者,您可以執行以下操作:
timer = setInterval(showRemaining.call(this), 1000);
其中分配this
里面showRemaining
到外this
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.