-
Notifications
You must be signed in to change notification settings - Fork 124
Numbas’ queueScript function for loading scripts
All of the Numbas-specific code (i.e. everything which isn’t an externally-produced library) is loaded asynchronously using the function Numbas.queueScript
, defined in runtime/scripts/numbas.js
.
Loading files asynchronously means that the browser does not lock up completely while loading scripts.
The idea is that the code in each file should be wrapped with a call to Numbas.queueScript
. The code is executed only when queueScript
decides it’s safe.
queueScript
takes care of dependency management, so that you can be sure everything you need is in place before your script is executed.
The arguments for Numbas.queueScript
are:
-
file
– the name of the file being loaded. This is so other files which depend on this file can know when it has been executed. -
deps
– an array of filenames which need to be executed before this file can be run. If a required file is in thescripts/
directory, you can just write its name without the directory or the.js
suffix. For example,['maths','util']
is the same as['scripts/maths.js','scripts/util.js']
. -
callback
– the code to be executed. Unless you have very good reasons for doing otherwise, this should be an anonymous function containing all of the code in the file.
A helpful side-effect of this system is that it ensures that all code is wrapped in an anonymous function, meaning it’s quite hard to pollute the global namespace.
There are now a million script-loading libraries for javascript, so I should probably replace my bespoke code with one of those, but for now queueScript
does everything that it needs to.