113408
113408

Reputation: 3444

How to call a function multiple times till user releases a button in AngularJS

I have a random function that I call when the user press a button. I would like to keep running that function till the user releases the button.

I found the mouse-down directive but it seems to not work for mobile.

I also tried the code below using the ng-touchstart lib.

HTML

 <div id="vs">
     <span ng-touchmove="randomize($event)" ng-controller="myCtrl">VS</span>
 </div>

Controller

.controller('myCtrl', function($scope) {
  $scope.randomize = function($event) {
    // trigger this function again and again till user releases the button
  };
})

With ng-touchmove the event is not even triggered.

Is there any way to achieve what I'm looking for ?

Upvotes: 3

Views: 242

Answers (1)

Jurjen Folkertsma
Jurjen Folkertsma

Reputation: 272

if the mouse-down worked for the desktop version, try using touchstart and touchend on mobile devices. touchmove will not work on a button press i think.

Ionic has several functions build in to detect if you are on mobile or desktop, use this to switch from touch to mouse-down events.

Try debugging the touch button using the chrome inspector and check what the error code is.

we ran into this issue before, we fixed it like this. Pretty sure this would be pretty much the same way in angular (we used this alongside angular)

              $('#function').on('mousedown touchstart', function (evt) {
              })

Ps. consider changing to a plugin with better documentation, i could´nt find any proper docs.

Upvotes: 0

Related Questions