I want to load an OWL file before executing other (visualisation-)scripts. To do this I tried everything from
$(document).ready
to
function visualize (file) {
if (!file)
{setTimeout(visualize(file), 2000)}
else
{jQuery(function($){visFeaturePool.init(file)})}}
I think it has to be possible with the setTimeout but that isn't working. I throws the error: Uncaught RangeError: Maximum call stack size exceeded, so it doesn't wait, it just recalls the visualize function untill the stack is full.
Does anybody know what I am doing wrong? Thanks!
Instead of
you want
or on modern browsers, you can provide arguments to pass to the function after the delay:
Those three explained:
visualize
immediately, and then passes its return value intosetTimeout
(and sincevisualize
calls itself, it keeps calling itself recursively and you end up with a stack overflow error).setTimeout
that, when called, will callvisualize
and pass it thefile
argument. The function we're passing intosetTimeout
will have access to thefile
argument, even though your code has run and returned, because that function is a closure over the context in which it was created, which includesfile
. More: Closures are not complicatedvisualize
function reference intosetTimeout
(note we don't have()
or(file)
after it) and also passesfile
intosetTimeout
(after the delay). On modern browsers,setTimeout
will pass that on to the function when calling it later.(There's an important difference between #2 and #3: With #2, if
file
is changed between whensetTimeout
is called and the timer expires,visualize
will seefile
's new value. With #3, though, it won't. Both have their uses.)setTimeout(visualize(file), 2000)
callsvisualize
immediately and passes its result tosetTimeout
, just like any other function call.Try this:
This way you are giving setTimeout an anonymous function that will be executed when scheduled, and you can pass parameters to visualize using a closure like
file
.will also work.