-
Notifications
You must be signed in to change notification settings - Fork 124
Scheduling tasks
In order to avoid locking up the browser when loading, most Numbas code is passed through Numbas.schedule
. This keeps track of a stack of jobs to perform, and ensures that the browser has a chance to update and take user input.
Anything that takes longer than about 200ms to execute should be split into jobs and passed through the scheduler.
Because Javascript supports closures, putting code in the scheduler is quite easy: wrap it in an anonymous function which you pass to Numbas.schedule.add
, like so:
Numbas.schedule.add(function() { <<your code>> });
If you need the this
variable to be a particular object, you can specify it with a second argument for Numbas.schedule.add
.
Jobs should be executed in the order you intend, so if you add a job inside another job, it will be executed straight after the parent job, instead of being put to the end of the global job stack. Note that you shouldn’t mix scheduled and non-scheduled code, because you can’t rely on a scheduled job being executed before reaching the non-scheduled code. There’s basically no cost to adding jobs though, so don’t worry about minimising the number of jobs you create.