如何在我的 AngularJS 应用程序中将变量的设置延迟 30 秒



我的应用程序中有以下代码:

putTestQuestionResponses()
   .finally(() => processingPutTestQuestion = false);

putTestQuestionResponses() 返回一个 promise。

有没有办法让我做到"

  • 如果承诺已解决:processingPutTestQuestion = false
  • 如果承诺被拒绝:延迟 30 秒,则处理 PutTestQuestion = false
你可以

这样做:

putTestQuestionResponses().then(
    //resolved
    function(){
        processingPutTestQuestion = false;
    },
    //rejected
    funciton(){
        $timeout(function(){
            processingPutTestQuestion = false
        }, 30000);
    }
);

并记得注射$timeout

您可以使用

.then块来解析承诺。如下所示:

如果您收到来自 AJAX 调用的响应:

    getResponsePromise.then(function(data){
      //this is the success case where the response is resolved
      processingPutTestQuestion = false ;
    }, function(err){
//this is the error block
       $timeout( function() {
     processingPutTestQuestion = false ;
    }, 30000)
    });

最新更新