• 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

    • Nice devices but my Surface Pro 9 got bricked from a Windows Update efter only three years. Never buying a device from MS again.
    • Icaros Shell Extensions 3.3.6 by Razvan Serea Icaros is a collection of lightweight, high quality, Windows Shell Extensions. Icaros can provide Windows Explorer thumbnails, for essentially any video media format supported by FFmpeg, this includes popular filetypes such as: Mkv, Flv, Avi, Mp4, Mov, Rmvb, M2ts, Ogm etc. Besides the FFmpeg supported filetypes, Icaros also contains custom parsers, which can produce Cover Art thumbnails for Mkv, Flac, Ape, Mpc and several other filetypes. Icaros also provides Windows Explorer properties for the following popular filetypes: Mkv, Mk3d, Mp4, Mov, Webm, Ogm, Ogg, Flv, Avi, Rm, Rmvb, Flac, Opus, Spx, Ape, Mp3, Mpc, Mka, Tak, Tta, Ofr, Wav and more. Properties refer to the info shown for each file in Explorer, such as length, width, height, title, channels and so on. Icaros works on Vista, Windows 7, Windows 8/8.1, Windows 10 and Windows 11. Icaros Shell Extensions 3.3.6 changelog: Added support for Animated WEBP thumbnailing Added support for UTF-8 filenames in CBX files (Comic book files) Added support for rotation of MKV thumbnails Added support for "Sort title" property in MKV files Fixed some wav files not showing properties Fixed some old h264 files producing green thumbnails Fixed default level of MKV tags (now set to 50) Updated FFmpeg Download: Icaros Shell Extensions 3.3.6 | 12.8 MB (Freeware) Download: Portable Icaros 3.3.6 View: Icaros Shell Extension Home Page | Screenshots 1 | 2 Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      Huge Trailer earned a badge
      Week One Done
    • Week One Done
      Classifyskilleducation earned a badge
      Week One Done
    • One Month Later
      eurospharma62 earned a badge
      One Month Later
    • Week One Done
      With What earned a badge
      Week One Done
    • Week One Done
      Harris Gilbert earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      536
    2. 2
      +Edouard
      167
    3. 3
      PsYcHoKiLLa
      70
    4. 4
      neufuse
      64
    5. 5
      ATLien_0
      63
  • Tell a friend

    Love Neowin? Tell a friend!