在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;
和下面的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);
答案 0 :(得分:1)
您将具有错误的代码行中的变量分配给不存在的内容。
@LargeTest
public class SmokeSetup extends LogInTestFixture {
@Rule
public ActivityTestRule<LoginActivity> mLoginActivity = new ActivityTestRule<>(LoginActivity.class);
@Test
public void testSetup() throws IOException {
onView(withId(R.id.username_field)).perform(replaceText("username"));
onView(withId(R.id.password_field)).perform(replaceText("password"));
onView(withId(R.id.login_button)).perform(click());
}
}
@LargeTest
public class LogoutTearDown extends LogInTestFixture {
@Rule
public ActivityTestRule<MainActivity> mMainActivity = new ActivityTestRule<>(MainActivity.class);
@Test
public void testLogout() throws IOException {
onView(withId(R.id.toolbar_menu)).perform(click());
onView(withId(R.id.logout_button)).perform(click());
}
}
相反,请确保您获得刚刚分配给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
。