question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

While loops make the browser freeze

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
PierreQuentelcommented, Mar 12, 2020

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 to False, but I would be surprised if it works.

0reactions
Animenosekaicommented, Mar 21, 2020

Great ! can we close the issue then ?

@PierreQuentel Yes!

Sent with GitHawk

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found