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

时间:2017-01-10 11:07:37

标签: javascript angularjs html5 ibeacon monaca

这是我的角度js文件(app.js),当我调用控制器(uuidCtrl)时,它将触发uuidCtrl控制器。当我想调用控制器2次时,我必须加载页面2次。但我想要在第一次调用后连续运行它。希望你有一些解决方案。

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">

1 个答案:

答案 0 :(得分:0)

您可以使用$ 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
});

希望这有帮助。