Javascript port and undefined references

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Devy
Posts: 1
Joined: Fri Sep 10, 2021 5:15 am

Javascript port and undefined references

Post by Devy » Fri Sep 10, 2021 5:20 am

Hi,

I complied the latest MicroPython Javascript port (https://github.com/micropython/micropyt ... javascript) using the instructions on the port page but keep getting 'Uncaught ReferenceError: mp_js_init is not defined at'

I am using the exact HTML on that page with micropython.js and surrounding files in 'build' so that all files can be found as expected.

Is there something I am missing? I complied the port with the default instructions provided (make), using latest emsdk on 20.04 Ubuntu.

TheMindVirus
Posts: 1
Joined: Tue Nov 09, 2021 2:00 pm

Re: Javascript port and undefined references

Post by TheMindVirus » Tue Nov 09, 2021 2:05 pm

I had the same issue, in 2021. After a bit of trial and error, I found the problem.

It is because even though window.load() or any global code should run after everything has been loaded or while it is loading,
Micropython bindings still haven't yet fully loaded by the time the browser reaches window.load().

A temporary solution is to use setTimeout() to trigger mp_js_init() 1000ms later and then it is defined and runs correctly.
A more acceptable solution is to use setInterval() to poll for if mp_js_init() has been defined yet or not when the browser is being slow.

Hope this helps. A.

Post Reply