Forum Moderators: open

Message Too Old, No Replies

JavaScript Text Scroller

         

ayushchd

1:08 pm on May 4, 2008 (gmt 0)

10+ Year Member



hi..i have the following function for scrolling the text..the function initializemarquee is initialized on the click of a button..the problem is, the more the user clicks on the button, the more faster the marquee becomes..i want that evrytime the function is initialized the speed of the marquee should remain the same..i have not been able to figure out what the problem is..please help..its urgent :

var delayb4scroll=0 //Specify initial delay before marquee starts to scroll on page (2000=2 seconds)
var marqueespeed=2 //Specify marquee scroll speed (larger is faster 1-10)
var pauseit=1 //Pause marquee onMousever (0=no. 1=yes)?

copyspeed=marqueespeed
var pausespeed=(pauseit==0)? copyspeed: 0

function scrollmarquee(){
if (parseInt(cross_marquee.style.left)>(actualwidth*(-1)+8))
cross_marquee.style.left=parseInt(cross_marquee.style.left)-copyspeed+"px"
else
cross_marquee.style.left=parseInt(marqueewidth)+8+"px"
}

function initializemarquee(){
cross_marquee=document.getElementById("vmarquee")
cross_marquee.style.left=0
marqueewidth=document.getElementById("marqueecontainer").offsetWidth
actualwidth=cross_marquee.offsetWidth

setTimeout('lefttime=setInterval("scrollmarquee()",30)', delayb4scroll)
}

Dabrowski

1:18 pm on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's obvious isn't it?

Every time the user clicks the button, another setInterval is triggered, the first one isn't cancelled. So if the user clicks 3 times, the scrollmarquee function is called 3 times per cycle.

The solution, you are capturing the interval id into lefttime with this command in the setTimeout function:

lefttime=setInterval("scrollmarquee()",30)

Now, add the line:

clearInterval( lefttime);

Before the setTimeout, this will cancel the first interval before the second is established. lefttime is then allocated the id of the second interval and so on....

You may have to add a global var lefttime at the top where you have pauseit and the others.

Dabrowski

2:27 pm on May 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Bump!

ayushchd, did that fix the problem?