javascript - SetInterval loop relatively confusing to me -
html
<div id="backspace" ng-click="deletestring(''); decrementcursor();">
js
<script> $scope.deletestring = function() { if($scope.cursorposval > 0){ //$scope.name = $scope.name - letter; $scope.name = [$scope.name.slice(0, $scope.cursorposval - 1) + $scope.name.slice($scope.cursorposval)].join(''); console.log($scope.name); settimeout(function(){ setcaretposition("inputbox", $scope.cursorposval); }, 30); } else { $scope.cursorposval = 1; } }; </script>
i designing on screen touchscreen keyboard. backspace button. going make when click , hold backspace button, starts removing characters automatically. don't know begin creating setinterval, , know setinterval need use here.
if i'm not wrong, want while you're keeping button pressed, function repeats itself.
you're right setinterval(). however, way manage event wrong.
take @ fiddle (it's not code, simple example best way understand):
http://jsfiddle.net/daq9atdd/1/
$(function(){ var interval = null; $('#mybutton').mousedown(function(){ interval = setinterval(function(){ console.log('hello !'); }, 250); }); $('#mybutton').mouseup(function(){ clearinterval(interval); }); });
i start interval when button pressed, store it, , clear when button released.
Comments
Post a Comment