Welcome Guest! To access all forums & features, please register an account or sign-in. → Why register?



onChange Event and setinterval


4 replies to this topic - - - - -

#1 Craig Hopson

    Neowinian

  • 60 posts
  • Joined: 17-November 12
  • Location: London
  • OS: Windows 8 :-(

Posted 01 December 2012 - 21:58

can this be done?

<!DOCTYPE html>
<html>
<head>
<script>
var seconds = 1000;

function test(optvalue) {
   var seconds = optvalue;
   alert(optvalue);

}

window.setInterval(function(){  
var d=new Date();
document.getElementById("output").innerHTML = d;
  }, seconds);
</script>
</head>
<body>
<select name="menu" onchange="test(this.value);">
	<option value="1">1</option>
	<option value="5">5</option>
	<option value="10">10</option>
</select>
<div id="output"></div>
</body>
</html>



#2 jamend

    Neowinian Wise One

  • 3,010 posts
  • Joined: 09-February 04
  • Location: Montreal, Canada

Posted 01 December 2012 - 22:10

There's a few issues. First, in test() you're doing var seconds = ...; Because you put var in front of it, it creates a variable within the local scope (which is the test function), and you're not actually changing the var seconds = 1000. Next, setInterval is not going to know or check that the seconds variable changed; it will continue using the interval that you originally gave it. You'd have to clear the existing interval and set a new one if you want to change it.

<script>
var seconds = 1000;
var interval;

function test(optvalue) {
   seconds = optvalue;
   alert(optvalue);
   window.clearInterval(interval);
   window.setInterval(myDateFunction, seconds);
}

var myDateFunction = function(){  
   var d=new Date();
   document.getElementById("output").innerHTML = d;
};

window.setInterval(myDateFunction, seconds);
</script>


#3 OP Craig Hopson

    Neowinian

  • 60 posts
  • Joined: 17-November 12
  • Location: London
  • OS: Windows 8 :-(

Posted 01 December 2012 - 22:19

thanks for the quick reply i tried your example but it didn't work...


[edit]
got it like this
<script>
var seconds;
var interval;
function test(optvalue) {
   seconds = optvalue;
   alert(optvalue);
interval = window.clearInterval(interval);
interval = window.setInterval(myDateFunction, seconds);
}
var myDateFunction = function(){ 
   var d=new Date();
   document.getElementById("output").innerHTML = d;
};
interval = window.setInterval(myDateFunction, seconds);
</script>


#4 jamend

    Neowinian Wise One

  • 3,010 posts
  • Joined: 09-February 04
  • Location: Montreal, Canada

Posted 01 December 2012 - 22:26

View PostCraig Hopson, on 01 December 2012 - 22:19, said:

thanks for the quick reply i tried your example but it didn't work
Whoops. Forgot to assign interval = setInterval()

<script>
var seconds = 1000;
var interval;

function test(optvalue) {
   seconds = optvalue;
   alert(optvalue);
   window.clearInterval(interval);
   interval = window.setInterval(myDateFunction, seconds);
}

var myDateFunction = function(){  
   var d=new Date();
   document.getElementById("output").innerHTML = d;
};

interval = window.setInterval(myDateFunction, seconds);
</script>

Also keep in mind that interval is set in ms, not seconds, so you might not get what you expect with your option values.

#5 OP Craig Hopson

    Neowinian

  • 60 posts
  • Joined: 17-November 12
  • Location: London
  • OS: Windows 8 :-(

Posted 01 December 2012 - 23:04

yes i found that thanks again