使用setInerval窗口时挂起

时间:2017-10-09 06:20:59

标签: javascript angularjs ionic-framework

 .controller('overviewCtrl', function ($scope, $http, $interval) {
        $scope.cardShow = false;
        $scope.infoShow = true;
        $scope.yes1 = false;
        $scope.yes2 = false;
        $scope.status = true;
        $scope.arbitration = true;
        $scope.notAccepted = true;
        $scope.cardShowRegular = false;
        $scope.check1 = false;
        $scope.id = 53;
        $scope.opportunitiesArr = [];
        $scope.opportunity = [];
        $scope.profile = [];
        /*getting opportunities*/

        $scope.oppList = function () {
            $http.get("https://serviceme.blynksystems.com:6443/createContract/fseoppurtunities/" + $scope.id)
                .then(function (response) {
                    $scope.opportunitiesArr = response.data;
                    console.log("opportunities " + JSON.stringify($scope.opportunitiesArr));
                }, function (response) {
                    console.log("error" + response);
                });
        }

        /*$rootScope.$broadcast("sendOpportunities", $scope.opportunitiesArr);*/
        $scope.oppList();
        /*end of getting opportunities*/
        $scope.contactInfo = function (opport) {
            $scope.opportunity = opport;
            console.log("opportfjdufiu" + JSON.stringify($scope.opportunity));
            console.log("opport " + JSON.stringify(opport));
            /*$scope.customerId = opport.customer_id;*/
            /*$scope.serviceName = opport.service_name;
            $scope.carModel = opport.car_model;*/
            /*$scope.driverEmail = opport.driver_email;*/
            /*$scope.statusOfFse = opport.status_of_fse;*/
            $scope.cardShow = true;
            $scope.cardShowRegular = true;
            $scope.infoShow = false;
            /*posting the acceptance*/
            if (opport.driver_email != null || opport.driver_email != undefined) {
                var data = {
                    "mechanic_id": $scope.id,
                    /*"customer_id": opport.customer_id,*/
                    "driver_email": opport.driver_email,
                    "service_name": opport.service_name,
                    "car_model": opport.car_model,
                    "status": 2,
                    "counter_price_fse": ""
                }
            } else {
                var data = {
                    "mechanic_id": $scope.id,
                    "customer_id": opport.customer_id,
                    /*"driver_email": opport.driver_email,*/
                    "service_name": opport.service_name,
                    "car_model": opport.car_model,
                    "status": 2,
                    "counter_price_fse": ""
                }
            }
            console.log("emergency" + JSON.stringify(data));
            var req = {
                method: 'POST',
                url: 'https://serviceme.blynksystems.com:6443/createContract/updatewhenfseaccepted',
                data: data
            }
            $http(req).then(function (response) {
                console.log("respose" + JSON.stringify(response.data));
            }, function (response) {
                console.log(response);

            });
            $scope.callFtn = function () {
    setInterval($scope.contactInfo(opport), 3000);
}
$scope.callFtn();
            /*$interval(function () {
    $scope.contactInfo(opport)
}, 3000);*/
            /*posting the acceptance*/

        }
        });

我正在研究离子项目。我需要在来自服务器的响应中每隔5秒检查一次状态。因为我在使用setinterval.But时使用这种方法我的窗口正在被攻击。我也是我试过setTimeOut尽管问题正在重复。我怎么能解决这个问题。这是我的代码:

1 个答案:

答案 0 :(得分:0)

您以递归方式使用setInterval,最终导致窗口卡住。请参阅此处了解替代解决方案:https://stackoverflow.com/a/18687829/1912288

请在Angular中使用$interval,因为它会在调用$interval时查找范围更改并反映绑定的更改。这是通过触发摘要周期来完成的。