View Full Version : clearTimeout() function won't work

03-07-2005, 04:35 PM
hi, it's me again!

i have a set of images next to each other on a div in an iframe. if i klick a link i'd like the layer to scroll one image and not more. unfortunately my clearTimeout() function won't work it scrolls through all images when i click only once, despite my stopfunction... can you see what's wrong?

after the i = 0 i call stopscroll() *but it won't stop scrolling :(


function moveleft() {

if (window.rightvar) {

if(parseInt(scrollframename.document.getElementById('scroll').style.left) == 0
|| parseInt(scrollframename.document.getElementById('scroll').style.left) == -649
|| parseInt(scrollframename.document.getElementById('scroll').style.left) == -1298
|| parseInt(scrollframename.document.getElementById('scroll').style.left) == -1947
|| parseInt(scrollframename.document.getElementById('scroll').style.left) == -2596
|| parseInt(scrollframename.document.getElementById('scroll').style.left) == -3245)
{ i=0


z = 2.135*i*Math.pow(e,-1/8*i)
scrollframename.document.getElementById('scroll').style.left = parseInt(scrollframename.document.getElementById('scroll').style.left)-speed*z+"px"
leftvar = setTimeout("moveleft()",update)


function stopscroll() {
if (window.leftvar)
if (window.rightvar)

03-07-2005, 05:41 PM
Are your variables rightvar and leftvar defined within the function moveleft()? If they are then your stopscroll() function does not have access to them as they are local to the script they are defined in. You need to make them global variables by defining them outside of a specific function.

03-07-2005, 05:55 PM
Please, give us a link to an example of this script in action.

Some tips, though:
- The function setInterval is smoother than setTimeout, because the timing doesn't get affected by the execution time.
- Store away the scrollframename.document.getElementById('scroll') reference in a variable (e.g. oElm) instead of looking it up time after time.
- Also, store away the parseInt(oElm.style.left) part in another variable (e.g. iPos).
- Instead of comparing to 0, -649, -1298 etc, do iPos%649==0.
- Use -0.125 instead of -1/8.
- You can use a single scroller function instead of one right and one left scroller if you pass a simple argument (e.g. iDir) that is either -1 for left or 1 for right, and replace the -speed part with +speed*iDir.

03-07-2005, 06:07 PM
thanks for the hints, it has been a while since i learned to programm ;)

here's a link to the script in action:


i defined the leftvar and rightvar so it's global, but it still scrolls through all of the images rather then through just one

can you figure out why?

03-07-2005, 07:11 PM
i always get en error when doing this:

var oElm = scrollframename.document.getElementById('scroll')
var iPos = parseInt(oElm.style.left)

it says: scrollframename not defined :(

but when i'm addressing it directly, it's no problem... :confused:

edit: when i'm doing it like this:

var iElm = document.getElementById('scrollframename')

i get an iElm.hastnoProperties error...

03-07-2005, 07:48 PM
Oh, I forgot that. Iframes are a little peculiar.
oIframeDoc=oIframe.contentDocument||oIframe.document;Here you must note that neither of these work in the Safari browser.

03-07-2005, 07:57 PM
oh damn, i'm using safari... is there no other way to do this?

did you see what was my error with scrolling? :confused:

03-08-2005, 12:49 AM
i'm starting to hate javascript :(

when i scroll to the left with z = 2.135*i*Math.pow(e,-1/8*i)

and then back to the right with the very same forumla, well, guess what, it doesn't scroll the same distance!

i'm really confused now.

03-08-2005, 05:40 PM
ok, i did it!

but it only scrolls to the left side because javascripts didn't manage to get my additions right (don't know why..) and i was too stupid to figure something better out :rolleyes:

style.left = parseInt(style.left) - speed * function scrolls to the left, whereas style.left = parseInt(style.left) + speed * function scrolls to the right, but with a very strange distance.

although it should be the same distance, it scrolls much more, maybe it has something to do with how addition of float units (function is an infinite number) works.

anyway, i found a page, where this problem has been solved (but i don't know how). you may want to look into it, it's really nice (though 4 years old i think).

the page is: http://www.kandahar-vintage.ch/history.htm

maybe some of you figure out how they did it and post a free dhtml script ;)

thnx anyways, good forum here.