• 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
  On 01/12/2012 at 22:19, Craig Hopson 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>
[/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

    • ALL surveys are voluntary and very few attempt to track the same percipients over time. That doesn't mean they are invalid. Value doesn't claim it is an aggregate of their entire user base, that is why they call it as survey, which means exactly what it is. Yes, if Value created an anonymized collection of ALL users, that would be more accurate than a survey, but that is something they choose not to do. Just like elections are more accurate than political surveys.
    • Just use AI...chatgpt or Copilot will write code for you, then test that and tweak it if needed.
    • One UI 8 reveals Samsung's upcoming tri-fold phone by Taras Buria Samsung Galaxy Flex G prototype shown earlier this year. Source: Samsung Samsung was the pioneer of mainstream foldable phones. Despite the rocky start, the company cemented itself as the go-to option for those who want something more exciting than the standard, boring slab phone. However, during recent years, Samsung foldable phones became a bit stagnant and a target of heavy criticism in light of China-made ultra-thin devices and even tri-folding phones. Now, Samsung is getting ready to strike again. The South Korean giant is holding a special event next week where it is expected to unveil a bunch of new folding phones, including a cheaper Z Flip FE variant. However, the most exciting part of the announcement could be Samsung's first tri-folding smartphone, details about which were discovered in the latest One UI 8 beta build. The uncovered animations reveal a device that is similar to the Galaxy Fold 6 with one extra screen. It shows a tri-camera module and the location of its NFC chip. The inner display houses a punch-hole camera in the upper-right corner. Judging by the looks of it and another punch-hole camera, it appears that when fully unfolded, the outer screen will sit between two panels. Samsung seems to be taking a different route than Huawei, equipping its smartphone with two inward-folding hinges (as shown by the recent Galaxy Flex G concept). It is worth noting that those hinges are not identical, and the phone will have a strict folding procedure. Another animation revealed the phone displaying a warning not to fold the camera side first to prevent screen damage from the camera island. The device name is currently unknown, and the One UI 8 beta build does not reveal much except for "MULTIFOLD7" with its "vertical main," "main," and "reverse main" displays. The exact naming and renders could leak days ahead of the presentations, so Samsung fans should stay tuned for more details. Source: Android Authority
    • In the civilized world, yes. But not in the US of A...
    • The entire western economy is based on the lie that internet ads lead to real sales.
  • Recent Achievements

    • Week One Done
      Devesh Beri earned a badge
      Week One Done
    • Week One Done
      956400 earned a badge
      Week One Done
    • First Post
      loose_observer earned a badge
      First Post
    • Week One Done
      BeeJay_Balu earned a badge
      Week One Done
    • Week One Done
      filminutz earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      449
    2. 2
      ATLien_0
      158
    3. 3
      +FloatingFatMan
      150
    4. 4
      Nick H.
      65
    5. 5
      +thexfile
      62
  • Tell a friend

    Love Neowin? Tell a friend!