页面加载后Javascript开始设置间隔
我有一个脚本,在显示隐藏div之前显示秒页面加载后Javascript开始设置间隔,javascript,Javascript,我有一个脚本,在显示隐藏div之前显示秒 var seconds_left = 20; var interval = setInterval(function() { document.getElementById('timer_div').innerHTML = "Or wait " + --seconds_left + " seconds to play the video."; if (seconds_left <= 0) { //alert('The
var seconds_left = 20;
var interval = setInterval(function() {
document.getElementById('timer_div').innerHTML = "Or wait " + --seconds_left + " seconds to play the video.";
if (seconds_left <= 0)
{
//alert('The video is ready to play.');
$('#def').fadeOut('slow');
clearInterval(interval);
}
}, 1000);
var秒\u左=20;
var interval=setInterval(函数(){
document.getElementById('timer\u div')。innerHTML=“或等待”+--seconds\u left+“秒播放视频。”;
如果(还剩几秒钟尝试在window.onload=function(){alert(“it's loaded!”)}
示例
window.onload = function () {
someFunction();
}
function someFunction() {
var seconds_left = 20;
var interval = setInterval(function () {
document.getElementById('timer_div').innerHTML = "Or wait " + --seconds_left + " seconds to play the video.";
if (seconds_left <= 0) {
//alert('The video is ready to play.');
$('#def').fadeOut('slow');
clearInterval(interval);
}
}, 1000);
}
window.onload=函数(){
someFunction();
}
函数someFunction(){
var seconds_left=20;
var interval=setInterval(函数(){
document.getElementById('timer\u div')。innerHTML=“或等待”+--seconds\u left+“秒播放视频。”;
如果(还剩几秒您可以在您的body标签上设置一个“onload”事件,该事件在您的内容完成加载后立即运行:
<body onload='yourFunction()'>
您不应该直接分配onload,它将替换任何现有的onload。onload经常使用,因此覆盖它是一个坏主意。执行以下操作:
window.addEventListener?
window.addEventListener(“加载”,yourFunction,false):
window.attachEvent&&window.attachEvent(“onload”,yourFunction);
如果您确实想确保页面已完全加载,以下是最佳方法:
$(function() {
// work when all HTML loaded except images and DOM is ready
});
$(window).load(function() {
// this is come when complete page is fully loaded, including all
// frames, objects and images **/
});
我建议您使用load函数解决这个问题。如果您使用jQuery,祝您愉快,请执行以下操作:
$(function(){
//your code
});
当页面加载后,它的代码将运行。这只是为了让您不会在onload方法中使用臃肿的代码啊,我明白您的意思,我的错误,我想我复制了它。现在看起来不错,删除了我的注释;)