【问题标题】:How to cancel angularjs $timeout when user navigate away from the page当用户离开页面时如何取消angularjs $timeout
【发布时间】:2013-07-04 17:15:10
【问题描述】:

我使用$timeout 定期更新我的 angularjs 应用程序中一个页面上的信息。当用户离开此页面时,我想取消$timeout。有没有简单的方法来做到这一点?

【问题讨论】:

    标签: web-applications angularjs web timeout


    【解决方案1】:

    好的,我摸索后找到了解决办法:

    $scope.$on('$destroy', function() {
        $timeout.cancel(timeout);
    });
    

    或者对于 Angular 1.5 中的新组件语法:

    this.$onDestroy = function() {
        if (timeout) {
            $timeout.cancel(timeout);
        }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多