setInterval 或 setTimeout 的好(更好)替代品

发布于 2024-09-02 09:34:47 字数 1109 浏览 10 评论 0原文

我有一个带有一些不错的 UI (jQuery) 功能的母版页。这些选项之一是与我嵌入的 YouTube(或其他类似)对象进行交互。在本例中,在每个 setInterval 事件中,嵌入视频都会停止显示新帧(大约一秒钟)。

更多详细信息: 我有一个“宝丽来”画廊(在标题中),其中只有 5 个 100x100 图像(测试:预加载对性能没有影响),并且我的画廊将在一段时间。 (测试:非动画显示:隐藏或显示:阻止对性能没有影响)。

经过一些测试,我得出的结论是,这不是图片的“动画”显示或隐藏,而是间隔本身(因为更改为显示:隐藏或阻止具有相同的结果)。也许这是我的“画廊”“功能”本身......

 function poladroid() {
     if (!galleryHasFocus) {
         if (galleryMax >= 0) {
             galleryCurrent++;

             if (galleryCurrent > galleryMax) {
                 galleryCurrent = 0;
                 showPictures = !showPictures;
             }
             if (showPictures) {
                 $('#pic-' + galleryCurrent.toString()).show("slow");
             }
             else {
                 $('#pic-' + galleryCurrent.toString()).hide("slow");
             }
         }
     }
     if (!intervalSet) {
         window.setInterval("poladroid()", 3000);
         intervalSet = true;
     }
 }

这并不是说我的功能正在做一些非常尴尬的事情,是吗?所以,我想我需要一个更“宽松”的间隔函数..但是有没有选择呢?

噢..差点忘了提一下:FireFox 和 Chrome 的表现相当不错;使用 IE(还有什么)的性能问题最多。

I've got a masterpage with some nice UI (jQuery) features. One of these options is interefering with my embedded YouTube (or other alike-) objects. On each, in this case, setInterval event the embedded video stops displaying new frames (for like a second).

More detail:
I've got a "polaroid" gallery (in the header) with only 5 100x100 images in it (test: preloading has no effect on performance) and my gallery will show or hide them (fade-in / fade-out) after a period of time. (test: non-animated display:hide or display:block has no effect on performance).

After some testing and I've come to the conclusion that it isn't the "animated" showing or hiding of the pictures, but it's the interval itself (- since altering to display:hide or block had the same result). Perhaps it is my "gallery" "function" on itself ...

 function poladroid() {
     if (!galleryHasFocus) {
         if (galleryMax >= 0) {
             galleryCurrent++;

             if (galleryCurrent > galleryMax) {
                 galleryCurrent = 0;
                 showPictures = !showPictures;
             }
             if (showPictures) {
                 $('#pic-' + galleryCurrent.toString()).show("slow");
             }
             else {
                 $('#pic-' + galleryCurrent.toString()).hide("slow");
             }
         }
     }
     if (!intervalSet) {
         window.setInterval("poladroid()", 3000);
         intervalSet = true;
     }
 }

It's not like my function is doing really awkward stuff is it? So, I was thinking I needed a more "loose" interval function.. but is there an option for it?

Ow.. almost forgot to mention it: FireFox and Chrome perform pretty good; using IE (what else) has the most performance problems.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时间海 2024-09-09 09:34:47

setInterval/setTimeout 是无可替代的 - 这些是在基于 ECMAScript 的浏览器中计时事件的唯一方法。

在这里理解真正的问题并不容易,但我猜你使用 setInterval 触发的任何内容都比它必须的要重(因为它是 jQuery) - 你应该尝试让它更重高效的。

如果您想要易于使用的幻灯片,请查看 http://github.com/oyvindkinsey/JsSlideshow - 它有一个演示 这里

顺便说一句,不要使用文字作为第一个参数要设置超时/设置间隔,请使用函数引用

setInterval(poladroid, 3000);

There is no substitute for setInterval/setTimeout - these are the only ways of timing events in browser based ECMAScript.

Its not easy to grasp the real issue here, but I'm guessing the whatever you are triggering using setInterval is heavier than it has to be (as it is jQuery) - you should try to make this more efficient.

If you want an easy to use slideshow, take a look at http://github.com/oyvindkinsey/JsSlideshow - it has a demo here

By the way, do not use a literal as the first argument to setTimeout/setInterval, use a function reference

setInterval(poladroid, 3000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文