I have a javascript code that needs to be executed after call to a library is complete, the library generates a canvas on the main page.I have already tried window.onload ,defer ,async, $(window).bind("load", function) but nothing seems to work. The aforementioned library makes a series of JS function calls within itself.I am open to using any plugin, any way as long as it allows me to execute my code after the page has loaded.
-
Which library are you using? It likely has defined its own events that you'll have to use.Jonathan Lonowski– Jonathan Lonowski2016年05月12日 18:07:26 +00:00Commented May 12, 2016 at 18:07
-
I am using bpmn.io bpmn io .iroh– iroh2016年05月12日 18:09:36 +00:00Commented May 12, 2016 at 18:09
2 Answers 2
There is no way to generically hook in to "When some arbitrary script has finished doing whatever it is doing".
You would need to modify the script so that it calls your function as the last thing it does.
If the script exposes its functions as globals, then you might be able to dynamically rewrite them.
e.g.
var original_make_canvas();
make_canvas = function make_canvas() {
original_make_canvas();
do_something_else();
}
... but there are a lot of ifs and buts to that and the above example would need a very simplistic case.
6 Comments
onload is entirely pointless there though, you can only assign a function to it and the return value of setTimeout is a number.window.onload=foo calls foo when the load event fires. window.onload = setTimeout(foo,0); calls set timeout immediately, assigns a number to onload (uselessly) and then calls foo after the minimum amount of time for a timeout has passed (or when the event loop becomes free, whichever occurs later)This is how I solved it. Suppose I have a function foo which needs to be executed after loading of certain element on the page. I used setinterval to call the function every 100 ms till it succeeds after which I used clearInterval. Something like Stop setInterval call in JavaScript.