repetedly call a axios.get to display data from en

2019-08-17 07:40发布

问题:

I working on backend written in flask that returns REST API for various task, one of which is to request deployment,

Then log gets filled in the database. So I have created another REST API endpoint to return log message for the specific request id. Now what I want is to once the showStatus is true the log_url used by axios.get should be constantly hitting and retrieving data from the backend endpoint retrieving data after 30 seconds. and show it in the show below as statusMessage.

<script>
import Vue from 'vue';
import axios from "axios";
import Multiselect from "vue-multiselect";


Vue.component("multiselect", Multiselect);

export default {
    name: 'SelectServices',
    data: function() {
        return {
            selectedService: "",
            services: [],
            updateExisting: "",
            showStatus: false,
            statusMessage : ""
        }
    },
    mounted() {

        console.log("running script");

    },
    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 ;

                    var log_url = "http://localwebsite.com:5060/services/get_logs/" + req_id;

                    axios.get(log_url)
                    .then(response => {
                                if (response.data.status == "success"){
                                    this.statusMessage = this.statusMessage + response.data.logs;
                                }
                            })
                })
                .catch((err) => {
                    console.log("Error happened: " + err.request.message);
                    alert(err);
                    return Promise.reject(err);
                })
                console.log(url);
                console.log(log_url);

        }
    }
}

based on the above logic I have written in vue it successfully request for deployment but the logs don't get printed in the UI! how can I constantly retrieve data from http://localwebsite.com:5060/services/get_logs/" + req_id after every 30 seconds.

回答1:

You can do something in the mounted hook:

mounted(){
    this.interval = setInterval(() => {
        axios.get(this.my_url).then(res => { /* do something */});
    }, 30000 );
},
data(){
    return {
        interval: undefined,
         my_url: undefined
    }
}

setInterval is a javascript function that allow you to run a function every predefined milliseconds. You can cancel it later with the following command: clearInterval(this.interval). You can also change my_url when you want :)