禁止setInterval进行REST调用

时间:2018-06-28 09:52:47

标签: javascript vue.js vuejs2 axios

我有this工作,但是现在我遇到了一个新问题:

<script>
import Vue from 'vue';
import axios from "axios";

export default {
    name: 'SelectServices',
    data: function() {
        return {
            selectedService: "",
            services: [],
            updateExisting: "",
            showStatus: false,
            statusMessage : "",
            interval : undefined
        }
    },
    mounted() {
      this.interval = setInterval(() => {
          axios.get(this.my_url).then(res => { /* do something */});
   }, 30000 );
}

    },
    methods : {
        selectServiceToDeploy: function(){
            // alert("micro services");
        },
        deploySelected: function(){

            this.showStatus = true ;
            // animate open the status window.
            $("#status_update").animate({height: '500'})
            var url = "http://localwebsite.com:5060/services/request_deploy";
                axios.post(url)
                .then(response => {
                    if (typeof response.data.reason != "undefined"){
                        alert("Recieved Status: " + response.data.status + ",\nReason: " + response.data.reason);
                    }
                    var req_id = response.data.result.request_id;
                    this.statusMessage = "Initiating deployment of Services for Request ID: " + req_id ;
                })
                .catch((err) => {
                    console.log("Error happened: " + err.request.message);
                    alert(err);
                    return Promise.reject(err);
                })
                console.log(url);
                console.log(log_url);

        }
    }
}

问题在于,即使显示了特定request_id的所有日志消息,setInterval仍在3秒后仍向axios.get发出log_url呼叫。一旦我从response.data.logs收到的log_url停止更新,或者状态设置为“已部署”,如何阻止该呼叫发生。在数据库中

1 个答案:

答案 0 :(得分:0)

您可以在需要停止请求时致电clearInterval(this.interval);