Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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 等待响应时间过长后,Angular promise停止工作_Javascript_Angularjs_Node.js_Express - Fatal编程技术网

Javascript 等待响应时间过长后,Angular promise停止工作

Javascript 等待响应时间过长后,Angular promise停止工作,javascript,angularjs,node.js,express,Javascript,Angularjs,Node.js,Express,我的应用程序向我的节点/express服务器发出GET请求。为了检查服务器代码中的更改,我添加了一个setTimeout函数。客户端代码中的promise函数可以正常工作几分钟,但如果从服务器获得回报的时间过长,最终会停止工作并抛出错误。有没有办法让promise函数一直运行,直到它从服务器返回数据,而不是立即抛出错误?基本上,我希望超时在服务器中保持运行并检查更改,我希望getMessageData()promise函数尽可能长时间地保持运行 这是angular应用程序中的工厂代码,用于从内置

我的应用程序向我的节点/express服务器发出GET请求。为了检查服务器代码中的更改,我添加了一个setTimeout函数。客户端代码中的promise函数可以正常工作几分钟,但如果从服务器获得回报的时间过长,最终会停止工作并抛出错误。有没有办法让promise函数一直运行,直到它从服务器返回数据,而不是立即抛出错误?基本上,我希望超时在服务器中保持运行并检查更改,我希望
getMessageData()
promise函数尽可能长时间地保持运行

这是angular应用程序中的工厂代码,用于从内置于nodeJS/Express中的远程服务器获取数据:

app.factory('getMessageData', ['$q', '$http', function($q, $http) {

    var last_updated_time = 0;

    return function getData() { 
        var deferred = $q.defer();

        console.log("Time stamp is: "+last_updated_time);
        var req = $http({
            url: 'https://20161128t161542-dot-jarvis-hd-live-2.appspot-preview.com/read-message',
            method: 'GET',
            cache: false,
            params: {last_updated: last_updated_time}
        });

        req.success(function(object) {
            last_updated_time = object.time_stamp;
            deferred.resolve(object);
        });

        req.error(function(err) {
            console.log("there was an error: "+err);
            deferred.reject(err);
        });

        return deferred.promise;
    }

}]);
这是我的指令代码,用于调用和启动工厂代码以发出请求:

app.directive('message', ['getMessageData', function(getMessageData) {
return {
    restrict: 'E',
    templateUrl: 'scripts/directives/message.html',
    link: function (scope) {

        function getMessage(){
            getMessageData().then(function(message_data){
                var message = message_data.data;
                scope.message = message;

                getMessage();

            }, function(err){
                console.log("Error: " + err);
            });
        }

        getMessage();

    } 
}
}]);
这是我的服务器代码,用于向/read邮件url发出GET请求:

app.get('/read-message', function(req,res){

        var last_updated_time = req.query.last_updated;

        function checkMessage() {
            if(!last_updated_time || last_updated_time < new_time){
                updateMessage();
            }
            else {
                setTimeout(function(){
                    checkMessage();
                }, 1000 * 10); //10 seconds
            }
        }

        function updateMessage() {
            var output = {
                success: 1,
                data: message,
                time_stamp: new_time
            };

            return res.json(output);
        }

        checkMessage();


    });
app.get('/read message',函数(req,res){
var last\u updated\u time=req.query.last\u updated;
函数checkMessage(){
如果(!last_updated_time | last_updated_time
在传入到$http的对象中,可以添加一个具有所需值(以毫秒为单位)的超时属性。
关于这个

你能说得更清楚些吗?我是否将配置对象添加到我的请求中<代码>var req=$http({url:'https://20161128t161542-dot-jarvis-hd-live-2.appspot-preview.com/read-message,方法:“GET”,缓存:false,参数:{last\u updated:last\u updated\u time})只需将超时添加到对象的末尾。var req=$http({url:'‌​w、 com/readmessage',方法:'GET',缓存:false,参数:{last\u updated:last\u updated\u time},超时:1000});这不管用。当我添加此超时时,我的应用程序会等待1秒,然后才会给出错误。这就是它的设置,1000ms=1秒。将其更改为最适合您的。通过将此超时添加到req函数,它是否每1秒运行一次该函数?无论我在什么时候提出,它都会辜负我的承诺。
promise
只解决或拒绝一次(与事件侦听器不同),因此您需要继续调用
getMessageData
,并返回一个新的
promise
对象-这篇文章可能会有帮助,并为您指明正确的方向,以便对此进行长时间轮询?