The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

setInterval JS Error

Hiya

I'm currently developing a script for a page of products on my site. When you load the products page the script is just meant to scroll naturally through the product images that are listed on that page. If a use hovers their mouse over a link for a product then the image scrolling should stop and show the relevant picture. This should stay on that image permanently until the user rolls off the link and then the auto scrolling should start again. The script works reasonably well so far:

The problem is that if you try to hover your mouse on the link before the page has finished loading I get an error saying "iIntervalID is undefined" as the setInterval has not returned a value to us yet.

This means that when the user hovers their mouse over the link the image will automatically flick on after a set amount of time rather than holding till the mouse is off the link.

Can anybody help me as i'm really stuck with where to go with this now,

The problem is that if you try to hover your mouse on the link before the page has finished loading I get an error saying "iIntervalID is undefined" as the setInterval has not returned a value to us yet.

This means that when the user hovers their mouse over the link the image will automatically flick on after a set amount of time rather than holding till the mouse is off the link.

You can remove the error like this

Code:

function stopCount()
{
if(iIntervalID)
clearInterval(iIntervalID);
}

but it will not halt scanning if you mouseover before the script runs.
Maybe the simplest fix for what you have in place, is never to cancel the interval. Instead the mouseover/out handlers should simply set/reset a flag that determines whether or not timedCount does anything.
Alternatively, check out PopSlide.

Thats Great!

Hiya

Yeah I did have an if statement to check that but it still gave the error as you described. I've changed the functionality so that the timed count has a flag check and if its set as false the function effectively doesn't do anything!

It works a treat with no errors so many thanks for your help, i'd been staring at it for so long I was running out of ideas!