While loops make the browser freeze
See original GitHub issueI’m trying to get the response of an api request made with ajax.ajax() and the response is stored into [‘apiResponse’] in the HTML5 Local Storage (but the rest of the python function processes without waiting for it be put into localStorage).
Because of this I need to wait for it before getting the response and I thought I could do what I did below for the program to wait before it proceed.
Unfortunately the browser seems to freeze every time I put a while loop…
If someone know how to make Brython and the browser to stop freezing or another method to do what I wanna do…
(It would really help me as it’s the only step before succeeding getting Spotify api requests response)
from browser import ajax #to make requests
from browser.local_storage import storage as localStorage #to access HTML5 Local Storage
import json #to convert a json-like string into a Python Dict
#Request to the API
def on_complete(req):
if req.status==200 or req.status==0:
localStorage['apiResponse'] = req.text
else:
print("An error occured while asking Spotify for data")
def apiRequest(requestUrl, requestMethod):
req = ajax.ajax()
req.bind('complete', on_complete)
req.open(requestMethod, requestUrl, True)
req.set_header('Authorization', localStorage['header'])
req.send()
def response():
while localStorage['apiResponse'] == '':
continue
print('done')
return json.loads(localStorage['apiResponse'])
Thanks in advance!
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
JavaScript "while" loop without freezing browser?
The problem is that the while loop is freezing the browser (and not even displaying the dialog, even though "open" is called first):...
Read more >How do I keep this loop from freezing the browser - Reddit
I have a gameloop function for my program, I tried putting it into a while(true) statement to make sure it was always running...
Read more >the while loop makes the browser freeze #1306 - GitHub
hi , i'm writing to report a bug, for a while now , i noticed that the while loop makes any of my...
Read more >Browser Freezing When Submitting An Exercise
The most common reason that browsers crash when you submit code is that you have mistakenly written an infinite loop in your code....
Read more >Site freezing on While Loops - JavaScript
Your browser is freezing because it's an infinite loop. The condition tests whether x is greater than zero OR less than or equal...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I understand that it’s how you would like the program to run, but I’m afraid it’s not possible. Sending an Ajax request doesn’t block the program until the request completes and then executes the next instruction - unless the API supports blocking Ajax calls, ie with the third parameter of
req.open()
set toFalse
, but I would be surprised if it works.@PierreQuentel Yes!
Sent with GitHawk