Forum Moderators: open

Message Too Old, No Replies

Help with setTimeout

         

Nalum

12:36 pm on Feb 9, 2007 (gmt 0)

10+ Year Member



Hey all,
I'm trying to set up a script to scroll some text in a div.
currently I have it scrolling the text but you have to keep moving the mouse pointer over the image to scroll it.

I want to be able to use the setTimeout function to allow the user to leave the mouse pointer over the image and have the text continue to scroll.

But the way that I've built the scroll function requires that the name of the div ( which the text is in ) is passed into the function using getElementById().

When I've used setTimeout it has been like this:
setTimeout(scrollUp(div),1); <- This one give me an error saying that i didn't wrap the function name in "".
setTimeout("scrollUp(div)",1); <- This one doens't do anything.
setTimeout("scrollUp("+div+")",1); <- This one says "missing ] after element list"

Is there a way that I can get it to work with the function argument?
Thank you for your time.

daveVk

1:06 pm on Feb 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



var div = "myDiv";
setTimeout("scrollUp('" + div + "')",1);

OR

setTimeout("scrollUp('myDiv')",1);

Assuming scrollUp takes name of div, and div called myDiv

Nalum

2:28 pm on Feb 9, 2007 (gmt 0)

10+ Year Member



Thanks for your reply.
This is the function that I have.

function scrollUp(div)
{
// we first have to get the position of the div.
var topOfDiv = div.scrollTop;
var heightOfDiv = div.scrollHeight;

// now we have to check that the div when scrolling up doesn't scroll past the topOfDiv value.
if(topOfDiv > 0)
{
div.scrollTop = div.scrollTop - (div.scrollAmount = 20);
setTimeout("scrollUp('"+div+"')", 1);
}
}

I'm trying to make it so that it can be run anywhere but it wont do the continuous scroll because div is gotten when using the onmouseover="scrollUp(getElementById('divName')".

I tried what you posted but it didn't work unfortunately.

Fotiman

3:28 pm on Feb 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The problem is that div seems to be an object instead of a string value, and it wont be in scope when your setTimeout function is called. One option might be to store the div object in a variable that is globally accessible, and then handle storing the div outside of your function call. For example:

// Global variable. Set this before calling scrollUp
var divToScroll = null;
function scrollUp()
{
if(!divToScroll ) return;
// we first have to get the position of the div.
var topOfDiv = divToScroll.scrollTop;
var heightOfDiv = divToScroll.scrollHeight;
// now we have to check that the div when scrolling
// up doesn't scroll past the topOfDiv value.
if(topOfDiv > 0)
{
divToScroll.scrollTop = divToScroll.scrollTop - (divToScroll.scrollAmount = 20);
setTimeout("scrollUp()", 1);
}
}

daveVk

11:25 pm on Feb 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is what I had in mind

function scrollUp(divId)
{
div = document.getElementById( divId );
// we first have to get the position of the div.
var topOfDiv = div.scrollTop;
var heightOfDiv = div.scrollHeight;

// now we have to check that the div when scrolling up doesn't scroll past the topOfDiv value.
if(topOfDiv > 0)
{
div.scrollTop = div.scrollTop - (div.scrollAmount = 20);
setTimeout("scrollUp('"+divId+"')", 1);
}
}

As Fotiman said you could use a global variable instead, this would limit you to one active scroll at a time, which in this case is probably Ok. I assume the div has an Id. eg <div id="myDiv" >...