我有这段代码,开始调用的时间间隔:间隔角清晰后取消
// Start timer function
var startTimer = function() {
// Apply our first image
applyImage(element);
console.log('about to start the timer');
console.log(timer);
// If our timer is not running
if (!timer) {
console.log('start timer');
// Start our timer
timer = $interval(changeImage, 10000);
}
};
有一件事我做的有检查,看看是否计时器以外的任何其他空/未定义。 但我注意到,当我打电话给我的停止功能:
var stopTimer = function() {
console.log('about to stop the timer')
// If we have started our timer
if (timer) {
console.log('stop timer');
// Stop it
$interval.cancel(timer);
}
};
实际上它并不清楚定时器它只是将状态设置为“取消”。 我呼叫$ interval.cancel(定时器)我可以设置定时器为空? 或者我应该改变我的启动功能,这样的事情:
// Start timer function
var startTimer = function() {
// Apply our first image
applyImage(element);
console.log('about to start the timer');
console.log(timer);
// If our timer is not running
if (!timer || timer.$$state.status === 2) {
console.log('start timer');
// Start our timer
timer = $interval(changeImage, 10000);
}
};
它不会以这种或那种方式改变很多。这是开发人员的偏好,希望在哪里更改代码。就个人而言,我会在取消定时器之后将'timer'设置为undefined,因为它可以更轻松地阅读和理解您在'startTimer()'函数中执行的操作。 –