• 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

    • "just $100 per TB"? Just? Are we trying to make this seem like the new normal? Kinda weird to make it sound like that is not a ridiculously expensive asking price.
    • The reviews you refer to mean nothing. Where there is no journalism there is no reason to call the gaming media's opinion pieces "reviews". For GP games there is indeed a metric for success - increasing subscriptions. Which turns in revenue. The only circumstance in which subs do not rise when great is being released is a Game Pass system where the company is close to fully saturated with customers in a subscription. However, in that case as the theory goes you spend aplenty in all kind of games - from shady live service cash cows and customer offending agitprop crap in purple colours to robust and entertaining single player games. And keep a solid level of profitability. Ignoring the simply innocuous but mid games MGS has released primarily of the second kind.
    • Report: Microsoft to use AWS to help GitHub deal with a major surge in demand by Pradeep Viswanathan Thanks to the surge of coding AI agents, GitHub's usage has skyrocketed over the past 12 months. To meet this demand, GitHub started with a plan in October 2025 to increase capacity by 10x. However, by early this year, the company realized that it needed 30x scale. This rapid growth has caused severe strain on the platform's reliability, resulting in several small outages over the past few months. In April, GitHub published a long blog post explaining the steps it is taking to resolve these reliability issues. In the post, the company also confirmed that it is working toward a multi-cloud architecture for better resilience. Today, Business Insider reported that GitHub is turning to Amazon Web Services to help deal with a major surge in AI-driven coding activity. It is important to note that GitHub is still in the process of moving completely to the Azure cloud. The current plan is to move the platform fully to Azure by 2027 so that it can scale better as per developer demand. Therefore, the current decision to utilize AWS might be part of a short-term plan to meet immediate demand. A Microsoft spokesperson confirmed that GitHub is using multiple cloud providers with the following statement: For Microsoft, the decision highlights the operational pressure behind the AI boom. GitHub has to stay reliable for developers at a time when rivals such as Codex, Cursor, Claude Code, and other AI coding tools are gaining attention. And the decision to use AWS for computing capacity seems practical given the circumstances.
    • It's growing on me, however, your right, it make better usability sense if the tabs were bellow the address bar.
    • Qwen 3.6 is better value per dollar, and you can run it locally for free.
  • Recent Achievements

    • Collaborator
      vjlex earned a badge
      Collaborator
    • Reacting Well
      Dys Topia earned a badge
      Reacting Well
    • Conversation Starter
      NovaEdgeX earned a badge
      Conversation Starter
    • One Year In
      Console General earned a badge
      One Year In
    • Week One Done
      Twozo Technologies earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      517
    2. 2
      +Edouard
      182
    3. 3
      PsYcHoKiLLa
      106
    4. 4
      Steven P.
      88
    5. 5
      ATLien_0
      68
  • Tell a friend

    Love Neowin? Tell a friend!