3

I am trying to make a non interactive display for a real estate shop window.

It's been a while since I've played with setInterval().

The first time my script steps through, it is fine. But when it tries to get the next property via getNextProperty(), it starts to go haywire. If you have Firebug, or an equivalent output of console.log(), you'll see it is calling things it shouldn't!

Now there is a fair bit of JavaScript, so I'll feel better linking to it than posting it all.

Store Display

Offending JavaScript

It is worth mentioning all my DOM/AJAX is done with jQuery.

I've tried as best to make sure clearInterval() is working, and it seems to not run any code below it.

The setInterval() is used to preload the next image, and then display it in the gallery. When the interval detects we are at the last image ((nextListItem.length === 0)), it is meant to clear that interval and start over with a new property.

It has been driving me nuts for a while now, so anyone able to help me?

It is probably something really obvious!

Many thanks!

Update

Well, now I feel better to know the problem was my naiveness of assuming plugins would work out of the box.

I did get this plugin from an answer on Stack Overflow. I have alerted the author of this question.

Thanks for all your answers!

4

1 回答 1

3

这是您的问题区域:

jQuery.extend(jQuery, {
  imagesToLoad: 0,

问题是这个变量是全局/共享的(jQuery.imagesToLoad),所以稍后检查:

if(jQuery.loadedImages.length >= jQuery.imagesToLoad){

取决于此插件不会同时被调用两次,否则如果检查出错,因为您稍后会在代码中调用它:

$.preloadImages({
  urls: [nextImage],

该计数下降到 1,使if评估true不仅在最后一张图像上,而且在第一张之后的集合中的第一张之后的所有图像(由于回调运行时,它在时间上重叠),这导致complete回调触发很多次,不只是一次,所以这段代码:

$.preloadImages({
 urls: preloadImages,
 complete: function() { showProperty(property); }
});

...看到该complete函数多次运行因此您同时启动了多个间隔(这就是您console.log('show property called' + property.slug)在日志中看到执行这么多次的原因)。

简短的修复:用preloadImages不使用全局变量的版本替换代码:)

于 2010-06-16T01:41:55.183 回答