简体   繁体   English

document.ready() 不起作用,但 setinterval() 起作用

[英]document.ready() doesn't work but setinterval() does

I don't really know why.我真的不知道为什么。 I have a page with lots of things that load external things.我有一个页面,里面有很多加载外部东西的东西。

Eg I have an slider that loads images with a script at the bottom of the page... I have another script tag at the bottom of it with some animations and stuff of kindness being applied to that slider.例如,我有一个滑块,它在页面底部加载带有脚本的图像......我在它的底部有另一个脚本标签,其中一些动画和善意的东西应用于该滑块。

The problem is that I tried to use a document.ready() , but it doesn't work... So I thought it was because the document is ready but the slider isn't, or something like that.问题是我尝试使用document.ready() ,但它不起作用......所以我认为这是因为文档已准备好但滑块没有,或类似的东西。

So, I put the same things I was executing in the document.ready() into a setInterval() refreshing every second... It worked, but the problem is that, even when I get what I want with that script, it keep refreshing!!!因此,我将我在document.ready()执行的相同内容放入每秒刷新一次的setInterval() ......它起作用了,但问题是,即使我用那个脚本得到了我想要的提神!!! And I don't want it, what I want is it to execute only once, but only when the page is really ready (When the refreshing animation in the tab of the browser stops) How can I do that?而且我不想要它,我想要的是它只执行一次,但只在页面真正准备好时(当浏览器选项卡中的刷新动画停止时)我该怎么做? Some idea?有什么想法?

You are confusing jquery's ready method with native onload.您将 jquery 的 ready 方法与本机 onload 混淆了。

Either use:要么使用:

window.onload = function(){
  // do something here. Everything including resources are loaded
}

or:或者:

$(document).ready(function(){
  // do something here. The DOM is loaded
});

Try to use window.onload = yourInitialisesFunction;尝试使用window.onload = yourInitialisesFunction; the onload event fireds when your DOM is completly loaded. onload 事件在您的 DOM 完全加载时触发。

(Sorry it was window.onload = yourFunction), JQueries ready() function fires when the DOM is parsed but not if all images etc is loaded. (抱歉,它是 window.onload = yourFunction),当解析 DOM 时会触发 JQueries ready() 函数,但不会在加载所有图像等时触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM