In my Flask App, when i click on the "Play" button, I want to display in the DOM some information processed by the server every 4 seconds for 'n' times. With "sync" AJAX POST it works, but isn't the best practice (freeze UI). Is there a better way to do that ? Thanks in Advance
Here is an Example
Flask.py
@app.route("/play", methods=['GET', 'POST'])
def play():
time.sleep(4)
# Do something
data_to_client = {}
return json.dumps(data_to_client)
Ajax POST
var i=0;
do{
i+=1;
$.ajax({
type: "POST",
url: "/play",
contentType: "application/json",
async: false, // method to change
data: JSON.stringify(data_to_server),
success: function(data_from_server){
// Every loop changes something in the DOM
}
});
}
while(i<=5)
HTML
<div class="col-md-auto p-3 border-top border-bottom">
<input type="submit" id="play" name="play" class="btn btn-success float-end" value="Play">
</div>
source https://stackoverflow.com/questions/70832981/flask-call-loop-ajax-post-when-click-on-a-button
Comments
Post a Comment