Adam Baser
Adam Baser

Reputation: 115

run javascript code after python if statement

I am trying to achieve the following. In my python code I have a if statement that checks for the response from a API server. if the response is 201 I want to run a code snippet inn javascript. The response is determined after a form is submitted in html.

I have then declared a variable globally and that variable is set to false. After the response is 201 i set this variable to true. If this variable is true i want to run a code snippet.

        submit = False
        if response == 201:
            submit = True
            response_page = redirect("next_url")
            # redirect to next step
            return response_page

Inn my javascript code I check for the variable like so:

    function setData(){
        if('{{submit}}'=="True"){
            #Run code
        }
    }

The setData() function is run when the form is submitted. But it doesn’t work because the function does not wait for the server response. Any one have any ideas on how I can achieve this?

Upvotes: 0

Views: 43

Answers (1)

Dovid Brailofsky
Dovid Brailofsky

Reputation: 43

I assume that u don't want to fetch it through js. maybe do setInterval() and clearInterval()

let setData;
if (!setData) {
setData = setInterval(function setData(){
    if('{{submit}}'=="True"){
        #Run code
        clearInterval(setData);
    }
  }, 1000);
}

I'm pretty new hope it helps.

Upvotes: 1

Related Questions