I'd like to have a button that calls a different function if it is pressed longer then 3 sec. Following code works fine with mouse events, but fails on mobile devices with touch events:
angular.module('myApp', []).controller('myCtrl', function($scope, $timeout) {
var mapService = {
setHome: function() {
console.log("setHome called");
},
goHome: function() {
console.log("goHome called");
}
};
var _homeDownTimeout = null;
var _homeWasSet = false;
$scope.homeDown = function() {
_homeDownTimeout = $timeout(function() {
mapService.setHome();
_homeWasSet = true;
}, 3000);
};
$scope.homeUp = function() {
if (_homeDownTimeout) {
$timeout.cancel(_homeDownTimeout);
}
if (!_homeWasSet) {
mapService.goHome();
} else {
_homeWasSet = false;
}
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<button class="button icon ion-home button-map" ng-mousedown="homeDown()" ng-mouseup="homeUp()">HOME</button>
</div>