【发布时间】:2017-10-06 06:22:40
【问题描述】:
我有一个使用 AngularJS 和 Onsen/Monaca UI 开发的跨平台应用程序。
我有一个功能可以监视按钮点击的变化,如果在按钮上检测到一定数量的点击,用户会被带到一个确认屏幕。
但是,如果用户选择按钮的时间过长,则应将它们重定向到另一个屏幕(尚未定义)。
我正在尝试使用此功能实现 $timeout 功能,但一旦用户选择了正确次数的按钮,我似乎无法取消 $timeout。如果用户在允许的时间内选择了按钮,他们将被带到确认页面,但在 10 秒后仍然显示 $timeout 消息。
以下是我的实现。可以假设一切正常 - 除了 stop() 函数中的 $timeout.cancel()。
// Initialise
var timer;
// Watch for changes on button clicks
$scope.$watch('currentAction', function(newValue, oldValue) {
if (counter == 6) {
// User clicked buttons - cancel the timer
stop();
// Segue to next page
Segue.goTo("confirmation.html");
}
else {
// Start the timer
timer = $timeout(function () {
alert("You are taking too long to respond");
}, 10000);
}
});
// Cancel the $timeout
function stop() {
$timeout.cancel(timer);
}
Segue.goTo() 只是将用户转到传入的页面(无关,但为了清楚起见包括在内)
var myFunctions = {
goTo: function (url) {
var nextPage = url;
var element = document.querySelector("ons-navigator");
var scope = angular.element(element).scope();
scope.myNavigator.pushPage(nextPage);
},
}
【问题讨论】:
-
你确认函数
stop曾经被执行过吗? -
是的,stop() 函数肯定被执行了。
-
请同时确认
timer不会在stop之后重新创建。 -
据我所知不是,它似乎没有通过代码重新创建。
标签: javascript angularjs timeout monaca