{{ val }}<\/p>\n<\/div>", js: "angular.module(\'MyApp\', [])\n .controller(\'MyCtrl\', [\'\$scope\', \'\$debounce\', function(\$scope, \$debounce) {\n \$scope.val = 0;\n \$scope.inc = function() {\n \$debounce(increase, 300);\n };\n\n var increase = function() {\n \$scope.val++;\n } \n }])\n // http://unscriptable.com/2009/03/20/debouncing-javascript-methods/\n // adapted from angular\'s \$timeout code\n .factory(\'\$debounce\', [\'\$rootScope\', \'\$browser\', \'\$q\', \'\$exceptionHandler\',\n function(\$rootScope, \$browser, \$q, \$exceptionHandler) {\n var deferreds = {},\n methods = {},\n uuid = 0;\n\n function debounce(fn, delay, invokeApply) {\n var deferred = \$q.defer(),\n promise = deferred.promise,\n skipApply = (angular.isDefined(invokeApply) && !invokeApply),\n timeoutId, cleanup,\n methodId, bouncing = false;\n\n // check we dont have this method already registered\n angular.forEach(methods, function(value, key) {\n if(angular.equals(methods[key].fn, fn)) {\n bouncing = true;\n methodId = key;\n }\n });\n\n // not bouncing, then register new instance\n if(!bouncing) {\n methodId = uuid++;\n methods[methodId] = {fn: fn};\n } else {\n // clear the old timeout\n deferreds[methods[methodId].timeoutId].reject(\'bounced\');\n \$browser.defer.cancel(methods[methodId].timeoutId);\n }\n\n var debounced = function() {\n // actually executing? clean method bank\n delete methods[methodId];\n\n try {\n deferred.resolve(fn());\n } catch(e) {\n deferred.reject(e);\n \$exceptionHandler(e);\n }\n\n if (!skipApply) \$rootScope.\$apply();\n };\n\n timeoutId = \$browser.defer(debounced, delay);\n\n // track id with method\n methods[methodId].timeoutId = timeoutId;\n\n cleanup = function(reason) {\n delete deferreds[promise.\$\$timeoutId];\n };\n\n promise.\$\$timeoutId = timeoutId;\n deferreds[timeoutId] = deferred;\n promise.then(cleanup, cleanup);\n\n return promise;\n }\n\n\n // similar to angular\'s \$timeout cancel\n debounce.cancel = function(promise) {\n if (promise && promise.\$\$timeoutId in deferreds) {\n deferreds[promise.\$\$timeoutId].reject(\'canceled\');\n return \$browser.defer.cancel(promise.\$\$timeoutId);\n }\n return false;\n };\n\n return debounce;\n }]);", css: "" }, fiddle: { version: "1", pastie_id: "1749055", slug: "6K7Kd", boilerplate: false, private: false }, panels: { html: "html", js: "javascript", css: "css" }, user: { id: "None", username: "", }, currentUser: false, isAuthor: false, features: { toggleSidebar: false }, is_pro: false } // translations const I18n = { editor: { panels: { result: "Result", drag_to_reorder: "Drag tabs to reorder", tidy: "Tidy the code with Prettify" }, sidebar: { toggle_sidebar: "Toggle sidebar" }, groups: { placeholder_value: "Add to collections", search_placeholder_value: "Search for collections", no_choices_text: "No more collections", no_results_text: "No collections found", item_select_text: "Press to select", you_have_no_groups: "You have no collections" } } } const EditorUISettings = { // options that user can change layout: 1, themeMode: 3, tabSize: 2, matchBrackets: true, lineNumbers: true, lineWrapping: true, keyMap: "default", autoCloseTags: true, autoCloseBrackets: true, indentWithTabs: false, codeLinting: true, autoRun: EditorConfig.currentUser ? false : false, autoRunValid: EditorConfig.currentUser ? true : false, autoSave: EditorConfig.currentUser ? true : false, clearConsole: false, fontSize: 1, matchTags: false, foldGutter: true, codeHints: true, editorConsole: true, hotreloadCss: true, hotreloadHtml: false } AngularJS Debounce Service - JSFiddle - Code Playground