Jonathan V.n. Jonathan V.n. - 5 months ago 9
Javascript Question

Change Var to 0 after button click

In my javascript I have an autoscroll wich is set to var speed 1. I want to set the var spped to 0 when there is clicked on a button. Somebody out there who can help me?

Button:

<button name="NONAUTO" value="">Turn off Autoscroll</button>


Here's my Javascript:

var speed=1


var currentpos=0,alt=1,curpos1=0,curpos2=-1

function initialize(){

startit()

}

function scrollwindow(){

if (document.all &&

!document.getElementById)

temp=document.body.scrollTop

else

temp=window.pageYOffset

if (alt==0)

alt=0

else

alt=0

if (alt==0)

curpos1=temp

else

curpos2=temp

if (curpos1!=curpos2){

if (document.all)

currentpos=document.body.scrollTop+speed

else

currentpos=window.pageYOffset+speed

window.scroll(0,currentpos)

}

else{

currentpos=0

window.scroll(0,currentpos)

}

}

function startit(){

setInterval("scrollwindow()",50)

}

window.onload=initialize


The autoscroll is interruptable, you can free scroll but if you can fix if you scroll and wait like 20 seconds and thenproceed the autoscroll that would be great!But this is just an extra.

Answer

you can do it like this.

 <button onClick = "turnOff(event)" name="NONAUTO"  value="">Turn off Autoscroll</button>

in the script file add this function.

var turnOff = function(event)
{
  event.preventDefault();
  speed = 0;
  alert(speed);
}