在AngularJS中立即启动$ interval

ale*_*exP 17 javascript jquery angularjs

我有notifications.get()一个每10秒运行一次的功能.

function notificationsCtrl($scope, $interval) {
    $interval(function(){ 
        $scope.notification = notifications.get();
        $('.card').addClass('rotate');
    }, 10000);
};
Run Code Online (Sandbox Code Playgroud)

但是当加载页面时,应该立即调用该函数.谁能帮我?

Pra*_*lan 47

你可以这样做,

  1. 将其定义为函数
  2. 通过功能名称立即调用它
  3. 将功能附加到$interval()每10秒执行一次.

码:

function notificationsCtrl($scope, $interval) {
    var fun = function(){ 
        $scope.notification = notifications.get();
        $('.card').addClass('rotate');
    };
    fun();
    $interval(fun, 10000);
};
Run Code Online (Sandbox Code Playgroud)