javascript: Best way to do the function after page load

user1128331 picture user1128331 · Sep 20, 2013 · Viewed 19.8k times · Source

I use google converted swiffy script like this

var stage = new swiffy.Stage(document.getElementById('swiffycontainer'),swiffyobject);
stage.start();

My point is I want to do the action after the animation end. but I don't know how to track the frame on swiffy object.

this is my work before asking

  • I put start method after page is loaded on

    $(document).ready

    $(window).load

    window.onload =

and track the time (random test to reach the last frame) with

setTimeout(callAtEnd(),10000);

result: not work; like it count the time when page is rendering, not after complete rendered

  • let it start method and put setTimeout on load (should count after complete rander

result: not work either

Please help or give other solution to do.

Many thanks.

Answer

nullability picture nullability · Sep 20, 2013

$(document).ready() will fire when the DOM is ready, but before the browser has finished loading all external resources.

window.onload will fire when the browser has finished loading all external resources. However, there may still be other operations being performed asynchronously.

Neither of these will give you a 100% accurate timing with relation to the swiffy timeline.

What you would need to do is add some ActionScript that will fire when the animation is complete. Since Swiffy only supports a subset of Actionscript, the simplest way is to have your Flash call a Javascript function by using the getURL() function, such as:

getURL("javascript:animationIsComplete();");

This way, Swiffy can call your completion handler directly without any need for timing page loads.

See Is there any way to detect when a Swiffy animation has completed?