Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS中的自动刷新_Javascript_Angularjs_Asynchronous - Fatal编程技术网

Javascript AngularJS中的自动刷新

Javascript AngularJS中的自动刷新,javascript,angularjs,asynchronous,Javascript,Angularjs,Asynchronous,在下面的代码中,在autorefresh上,计时器在发送所有查询时启动,但我希望计时器在所有ajax查询完成时启动。 我该怎么做呢 $scope.autoRefresh = function(){ $scope.running = !$scope.running; $scope.timeoutsec = 10; if($scope.running) { $scope.timer = setInterval($scope.refresh, 1

在下面的代码中,在autorefresh上,计时器在发送所有查询时启动,但我希望计时器在所有ajax查询完成时启动。 我该怎么做呢

    $scope.autoRefresh = function(){
    $scope.running = !$scope.running;
    $scope.timeoutsec = 10;
    if($scope.running)
    {
        $scope.timer = setInterval($scope.refresh, 10000);
        $scope.count = $timeout($scope.countdown,1000);
    }
    else
    {
        clearInterval($scope.timer); 
        $timeout.cancel($scope.count);
    }
}
倒计时函数--

    $scope.refresh = function(){
    div.style.visibility = "visible";
    $scope.update();
    $scope.timeoutsec = 11;
}
刷新功能--

    $scope.refresh = function(){
    div.style.visibility = "visible";
    $scope.update();
    $scope.timeoutsec = 11;
}
编辑: 更新功能--

    $scope.refresh = function(){
    div.style.visibility = "visible";
    $scope.update();
    $scope.timeoutsec = 11;
}
$scope.update()=函数(){

对于(var i=0;i如果使用xmlhttp请求,则必须将xhr.open()函数中的第三个参数设置为true。这将使请求同步,如下所示:

 xhr.open(method, url, true);

下面是一些样板代码,让您看看如何实现它

(function() { 
    'use strict';

    angular.module('app')

        .controller('controller', function($scope, $http, $timeout, $q) {
            $scope.update = function () {

                // single request
                $http.get('url')
                    .then(function (data) {
                        $timeout($scope.update, 10000);
                    });

                // or with multiple requests
                var request1 = $http.get(/****/);
                var request2 = $http.get(/****/);
                var request3 = $http.get(/****/);

                $q.all([request1, request2, request3])
                    .then(function() {
                        $timeout($scope.update, 10000);
                    });
            }; 
        });
})();

初始化$scope.running为false,仅当按下自动刷新按钮时,running才应设置为true

    $scope.running = false;
自动刷新函数-

    $scope.autoRefresh = function(){
    $scope.running = !$scope.running;
    $scope.timeoutsec = 10;
    if($scope.running)
    {
        $scope.count = $timeout($scope.countdown,1000);
    }
    else
    {
        $timeout.cancel($scope.count);
    }
}
倒计时函数--

    $scope.refresh = function(){
    div.style.visibility = "visible";
    $scope.update();
    $scope.timeoutsec = 11;
}
刷新功能---

惠纳尔函数--

    $scope.refresh = function(){
    div.style.visibility = "visible";
    $scope.update();
    $scope.timeoutsec = 11;
}
所有(承诺)时的功能{
变量i,数据=[],
dfd=$.Deferred();
对于(i=0;i<0.length;i++){
承诺[i].done(函数(newData){
data.push(newData);
if(data.length==promissions.length){
解析(数据);
}
}).fail(函数(){
数据推送(“无数据”);
if(data.length==promissions.length){
解析(数据);
}
});
}
返回dfd.promise();
}

将所有ajax查询推送到$scope.querys。

将所有$http ajax推送到一个数组中,就像这样,因为$http return承诺,所以可以使用$q API在所有请求完成时进行处理:

var httpPromises = [
    $http({method:'GET', url: '/myjson0.json'}),
    $http({method:'GET', url: '/myjson1.json'}),
    $http({method:'GET', url: '/myjson2.json'}),
    $http({method:'GET', url: '/myjson3.json'})
];


//handle your requests like you would normally do
httpPromises[0].success(...).fail(...);
httpPromises[1].success(...).fail(...);
httpPromises[2].success(...).fail(...);
httpPromises[3].success(...).fail(...);

// remember to inject $q in your controller/factory
$q.all(httpPromises,function(){
    //start your timer here
});

什么是ajax查询?请显示所有相关代码。此处不显示您的查询,但如果是我们正在讨论的
$http
$resource
请求(即返回承诺),这听起来像是
$q.all([promise1,promise2,promise3])。然后(回调);
var httpPromises = [
    $http({method:'GET', url: '/myjson0.json'}),
    $http({method:'GET', url: '/myjson1.json'}),
    $http({method:'GET', url: '/myjson2.json'}),
    $http({method:'GET', url: '/myjson3.json'})
];


//handle your requests like you would normally do
httpPromises[0].success(...).fail(...);
httpPromises[1].success(...).fail(...);
httpPromises[2].success(...).fail(...);
httpPromises[3].success(...).fail(...);

// remember to inject $q in your controller/factory
$q.all(httpPromises,function(){
    //start your timer here
});