Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add throttle argument for $watch #58

Closed
RavenHursT opened this issue Oct 3, 2014 · 3 comments
Closed

Add throttle argument for $watch #58

RavenHursT opened this issue Oct 3, 2014 · 3 comments

Comments

@RavenHursT
Copy link

So, I have a snippet of code that I keep copy and pasting all over my controllers for handling "auto-saving" functionality for objects:

var pendingPromise = null;
        //Set up watch to auto save CTA on changes, but throttled once every 500ms.
        $scope.$watch('myObject', function(nv, ov){
            if((nv && ov) && (!_.isEqual(nv, ov))){
                if (pendingPromise) {
                    $timeout.cancel(pendingPromise);
                }

                pendingPromise = $timeout(function () {
                    persistMyObject();
                }, 500, false);
            }
        }, true);

I do this so that the API the underlying service talks to doesn't get inundates w/ requests, especially for ui components like sliders that change the model incrementally very quickly. If would be wonderful if in some later release of Angular if this were condensed down to something like:

$scope.$throttledWatch('myObject', function(nv, ov){
    if(nv){
        persistMyObject();
    }
}, true, 500);

Where 500 is the time in milliseconds to set on the throttle.

@mhevery
Copy link
Contributor

mhevery commented Oct 8, 2014

This is the wrong repo. I think you may want to start on StackOverflow first or AngularJS repo.

@mhevery mhevery closed this as completed Oct 8, 2014
@RavenHursT
Copy link
Author

Oops.. sorry about that.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants