I have a cross platform app developed using AngularJS and Onsen/Monaca UI.
I have a function that watches for changes on button clicks and if a certain number of clicks have been detected on a button, the user is taken to a confirmation screen.
However, if the user takes too long to select the buttons, they should be re-directed to another screen (not yet defined).
I am trying to implement the $timeout function with this but I cant seem to cancel the $timeout once the user has selected the buttons the right number of times. If the user select the buttons in the allowed time, they are taken to the confirmation page, but then the $timeout message is still displayed after 10 seconds.
Below is my implementation. It can be assumed that everything works - except the $timeout.cancel() in the stop() function.
// 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);
}
Where the Segue.goTo() simply segues the user to the passed in page (unrelated but included for clarity)
var myFunctions = {
goTo: function (url) {
var nextPage = url;
var element = document.querySelector("ons-navigator");
var scope = angular.element(element).scope();
scope.myNavigator.pushPage(nextPage);
},
}