<!DOCTYPE html>
<html>

  <head>
    <link data-require="bootstrap-css@*" data-semver="3.3.1" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
    <script data-require="angular.js@1.3.16" data-semver="1.3.16" src="https://code.angularjs.org/1.3.16/angular.js"></script>
    <script data-require="ui-bootstrap@*" data-semver="0.13.0" src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.0.min.js"></script>
    <script data-require="lodash.js@3.10.0" data-semver="3.10.0" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.0/lodash.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-app="testApp">
    <div ng-controller="TestController as testCtrl">
      
      <p>
        Number of calls: {{numberOfCalls}}
      </p>
      
      <button ng-click="variableForWatch = !variableForWatch">Trigger $watch event</button>
      
      <br/>
      <i>Function for updating the number will be called after 500 ms from last hit on the button.</i>
    </div>
  </body>

</html>
var app = angular.module('testApp', []);

app.controller("TestController",['$scope',function($scope){
    $scope.variableForWatch = false;
    $scope.numberOfCalls = 0;
    $scope.logs = [];

    $scope.$watch('variableForWatch', function(newValue, oldValue) {
        $scope.incrementWithDebounce();
    });

    $scope.incrementWithDebounce = _.debounce(function() {
        incrementTestVariable();
    }, 500);

    function incrementTestVariable() {
      $scope.$apply(function() {
        $scope.numberOfCalls++;
      });
    };
}]);
/* Styles go here */