javascript:页面加载后执行该功能的最佳方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18920755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:41:30  来源:igfitidea点击:

javascript: Best way to do the function after page load

javascriptjquerygoogle-swiffy

提问by user1128331

I use google converted swiffy script like this

我像这样使用谷歌转换的 swiffy 脚本

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.

我的意思是我想在动画结束后做动作。但我不知道如何在 swiffy 对象上跟踪框架。

this is my work before asking

这是我问之前的工作

  • I put start method after page is loaded on

    $(document).ready

    $(window).load

    window.onload =

  • 我在页面加载后放置 start 方法

    $(document).ready

    $(window).load

    窗口.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
  • 让它启动方法并加载 setTimeout (应该在完成 rander 后计数

result: not work either

结果:也不起作用

Please help or give other solution to do.

请帮助或提供其他解决方案。

Many thanks.

非常感谢。

回答by nullability

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

$(document).ready()将在 DOM 准备好时触发,但在浏览器完成加载所有外部资源之前。

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

window.onload当浏览器完成加载所有外部资源时将触发。但是,可能仍有其他操作正在异步执行。

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

这些都不会为您提供与 swiffy 时间线相关的 100% 准确时间。

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:

您需要做的是添加一些在动画完成时会触发的 ActionScript。由于 Swiffy 仅支持 Actionscript 的一个子集,因此最简单的方法是让您的 Flash 使用该getURL()函数调用 Javascript函数,例如:

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

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

这样,Swiffy 可以直接调用您的完成处理程序,而无需计时页面加载。

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

请参阅是否有任何方法可以检测 Swiffy 动画何时完成?

回答by António Regadas

You can call it in two ways:

您可以通过两种方式调用它:

document.onload=function() {}

window.onload = function() {}

The best to use is document.onload because it separates content structure from code, but it all depends on the amout of javascript you have.

最好使用的是 document.onload,因为它将内容结构与代码分开,但这完全取决于您拥有的 javascript 数量。

If you want to use jQuery its:

如果你想使用 jQuery,它的:

$(document).ready() {}