使用对象引用调用 setinterval

发布于 2024-10-08 17:13:04 字数 1521 浏览 2 评论 0原文

我有一个横幅旋转器,我想使用对象而不是函数,这样我可以使代码更高效。无论如何,我似乎无法让 setInterval 工作。我认为这与对象引用有关。有人能解释一下吗?这是我到目前为止所得到的:

window.addEvent('domready', function() {
function set_banner(divid, array)
{
    var banner = $(divid);
    banner.set('html', '<a href=""><img src="" alt=""/></a>');
    var banner_link = $(divid).getElement('a');
    var banner_image = $(divid).getElement('img');

    var delay = 0;  

    for (var keys in banner1array) {
        var callback = (function(key) { return function() {
            banner.setStyle('opacity', 0);
            var object = array[key];
            for (var property in object) {
                if (property == 'href') {
                    var href = object[property];
                }
                if (property == 'src') {
                    var src = object[property];
                }
            }
            if (!banner.getStyle('opacity')) {
                banner.set('tween', {duration:1000});                   
                banner_link.setProperty('href', href);
                banner_image.setProperty('src', src);
                banner.tween('opacity', 1);
            }
        }; })(keys);
        setTimeout(callback, delay);
        delay += 21000;
    }
}

var banner1 = set_banner('banner1', banner1array);
setInterval(function() {set_banner('banner1', banner1array);}, 84000);

var banner2 = set_banner('banner2', banner2array);
setInterval(function() {set_banner('banner2', banner2array);}, 84000);

});

I have a banner rotator and I wanted to use objects instead of functions so I could make the code more efficient. Anyway I can't seem to get setInterval to work. I think it's got something to do with the object reference. Can anybody explain this? Here's what I've got so far:

window.addEvent('domready', function() {
function set_banner(divid, array)
{
    var banner = $(divid);
    banner.set('html', '<a href=""><img src="" alt=""/></a>');
    var banner_link = $(divid).getElement('a');
    var banner_image = $(divid).getElement('img');

    var delay = 0;  

    for (var keys in banner1array) {
        var callback = (function(key) { return function() {
            banner.setStyle('opacity', 0);
            var object = array[key];
            for (var property in object) {
                if (property == 'href') {
                    var href = object[property];
                }
                if (property == 'src') {
                    var src = object[property];
                }
            }
            if (!banner.getStyle('opacity')) {
                banner.set('tween', {duration:1000});                   
                banner_link.setProperty('href', href);
                banner_image.setProperty('src', src);
                banner.tween('opacity', 1);
            }
        }; })(keys);
        setTimeout(callback, delay);
        delay += 21000;
    }
}

var banner1 = set_banner('banner1', banner1array);
setInterval(function() {set_banner('banner1', banner1array);}, 84000);

var banner2 = set_banner('banner2', banner2array);
setInterval(function() {set_banner('banner2', banner2array);}, 84000);

});

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

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

发布评论

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

评论(2

别理我 2024-10-15 17:13:04

几个简单的错误:

 var banner1 = new set_banner('banner1');
               ^ ---------- creates a new object and uses set_banner as the constructor
                            your code already gets called here
                            and you get a new object back, which in this case has NO use

 ....
 setInterval(banner1(), 42000);
                     ^----------------- The parenthesis EXECUTE the function
                                        the RETURN VALUE is then passed to setInterval
                                        BUT... banner1() is NOT a function, so this fails

如果您想在 42 秒后调用 set_banner 并传递参数,您需要做的是使用匿名函数,然后该函数调用 set_banner

setInterval(function() { // pass an anonymous function, this gets executed after 42 seconds...
    set_banner('banner1'); // ...and then calls set_banner from within itself
}, 42000);

A couple of simple mistake:

 var banner1 = new set_banner('banner1');
               ^ ---------- creates a new object and uses set_banner as the constructor
                            your code already gets called here
                            and you get a new object back, which in this case has NO use

 ....
 setInterval(banner1(), 42000);
                     ^----------------- The parenthesis EXECUTE the function
                                        the RETURN VALUE is then passed to setInterval
                                        BUT... banner1() is NOT a function, so this fails

What you want to do in case that you want to call set_banner after 42 seconds AND pass a parameter is to use an anonymous function which then calls set_banner.

setInterval(function() { // pass an anonymous function, this gets executed after 42 seconds...
    set_banner('banner1'); // ...and then calls set_banner from within itself
}, 42000);
心意如水 2024-10-15 17:13:04

其他需要考虑的事情:http://zetafleet.com/blog/why-i-考虑设置间隔有害
(tl:dr 使用 setTimeout 代替 setInterval。)虽然我不确定他的论点是否适用于此,但养成避免的习惯似乎是一件好事。

function defer_banner(div, bannerArray, delay) {
  setTimeout(function() {
    setBanner(div, bannerArray);
    defer_banner(div, bannerArray, delay);
  }, delay); 
});

Something else to consider: http://zetafleet.com/blog/why-i-consider-setinterval-harmful.
(tl:dr Instead of setInterval, use setTimeout.) While I'm not sure that his arguments apply here, it seems like a good thing to get in the habit of avoiding.

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