如何在第一次调用后连续(每 5 秒)运行 一个 angular js 控制器

how to run a angular js controller continuously(in every 5 seconds) after called the first time

这是我的 angular js 文件(app.js)当我调用控制器(uuidCtrl)时它会触发 uuidCtrl Controller.when 我想调用控制器 2 次我有加载第 2 页 times.But 我想在调用第一个 time.Hope 后继续 运行 它 time.Hope 你有一些解决方案。

var app = angular.module('myApp', ['onsen']);

app.service('iBeaconService', function () {
    this.currentBeaconUuid = null;
    this.onDetectCallback = function () {
    };

    this.beacons = {
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx": {
            icon: 'img/1.jpg',
            rssi: -200,
            proximity: PROX_UNKNOWN,
            name: 'Beacon',
            number: '1',
            id: '000265C9'
        }

    };

    createBeacons = function () {
        //some code
    };

    this.watchBeacons = function (callback) {
        ///some code
    };

});
app.controller('uuidCtrl', ['$scope', 'iBeaconService', function ($scope, iBeaconService) {

    $scope.beacons = iBeaconService.beacons;

    var callback = function (deviceData, uuid) {

        iBeaconService.watchBeacons(callback);
    }
}]);

此 js 应在应用程序中循环以扫描附近的信标

这是我调用控制器的地方

<ons-page id="page2" ng-app="myApp" ng-controller="uuidCtrl">

您可以使用 $interval

app.controller('uuidCtrl', ['$scope', '$interval', 'iBeaconService', function ($scope, $interval, iBeaconService) {
  ...
  $interval(function() {
    // do whatever you want to execute every 5 sec
  }, 5000) // 5000 ms execution
  ...

此外,您可能想要调用 watchBeacons 并传递回调,而不是像现在那样简单地定义一个函数

$scope.beacons = iBeaconService.beacons;

iBeaconService.watchBeacons(function (deviceData, uuid) {
  // do whatever
});

希望对您有所帮助。