• 0

onChange Event and setinterval


Question

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>
[/CODE]

Link to comment
https://www.neowin.net/forum/topic/1123288-onchange-event-and-setinterval/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

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>
[/CODE]

  • 0

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>
[/CODE]

  • 0

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>
[/CODE]

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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Sound Booster 1.13 Build 575 by Razvan Serea Increase volume on your laptop. Boost the volume of even very quiet speakers. Raise volume above maximum. Letasoft Sound Booster can be used as an extra amplifier when the volume of the program you are listening to is too low for comfortable listening and you feel that your PC’s speakers can produce a louder sound. This can be the case when the audio or video has been recorded at the level below normal. Or, for example, when the other person’s microphone you are talking to on Skype, is not working properly. There can be a plenty of other reasons why the sound is being too quiet and there is not much you can do about it except buy external speakers. Sound of any application that is being played in the system can be amplified up to 500%. Letasoft Sound Booster can raise volume of programs like web browsers, on-line communication software (Teams, Discord, Zoom), any kind of media player or games. Simply put, volume of virtually any program that can play any sound in PC, can be boosted to a comfortable level with Letasoft Sound Booster. You can control current volume with the pop-up slider from the system tray. Alternatively, you can assign system-wide hot keys for increasing and decreasing, and use them when you need to adjust volume Letasoft Sound Booster constantly monitors current gain level and prevents samples from clipping, thus reducing all major sound distortions. Sound Booster 1.13 Build 575 changelog: usability improvements minor bug fixes Download: Letasoft Sound Booster 1.13.0.575 | 5.4 MB (14-Day Free Trial) View: Letasoft Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • I own these and the normal QuietComfort ones from Bose. The QC are amazing and I want to love these, but for the life of me, I have a terrible time getting them attached to my ears.
    • Thanks for the tip. I am giving it a try. Initial quick impression: I like it.
    • Welcome to Neowin. Enjoy your time here!
  • Recent Achievements

    • Conversation Starter
      NovaEdgeX earned a badge
      Conversation Starter
    • One Year In
      Console General earned a badge
      One Year In
    • One Year In
      Twozo Technologies earned a badge
      One Year In
    • One Month Later
      Twozo Technologies earned a badge
      One Month Later
    • Week One Done
      Twozo Technologies earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      503
    2. 2
      +Edouard
      183
    3. 3
      PsYcHoKiLLa
      106
    4. 4
      Steven P.
      89
    5. 5
      ATLien_0
      67
  • Tell a friend

    Love Neowin? Tell a friend!