-3

I am trying to create a random timer from 10 to 60 seconds with half-second interval. But my code even though it looks to me that it should work does not work so I need someone else to tell me what is wrong.

function timer(t) {
    while (t >= 0) {
        setTimeout(function() {
            t--;
        }, 500);
    }
    alert("Time OUT!");
    console.log(t);
    return;
};
$("playbutton").click(function() {
    var randomTime = Math.round(Math.random() * 100 + 20);
    timer(randomTime);
    alert(randomTime);
});
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
The S1ash
  • 3
  • 1
  • 1
    Welcome to Stack Overflow! *"Don't downvote unless you can help!"* Over nine years on SO tell me that that is not going to have the effect you want it to have, it's going to have the **opposite** effect if anything. (And it doesn't belong in the question either way, it's not about your problem.) – T.J. Crowder Dec 24 '18 at 14:57
  • Please take the [tour] (you get a badge!), have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) I also recommend Jon Skeet's [Writing the Perfect Question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and [Question Checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). *"even though it looks to me that it should work..."* What do you mean by "work"? What do you mean by "work"? What do you want the code to do? – T.J. Crowder Dec 24 '18 at 14:59
  • Related reading: http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call and https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron – T.J. Crowder Dec 24 '18 at 15:00
  • Thanks for making is so incredibly difficult. This is exactly the reason I never asked anything on SOF... I gave so simple question and got so incredibly long answer. – The S1ash Dec 24 '18 at 15:11
  • Can you tell us what “it doesn’t work” means? Just by looking at this, I’d guess that what you’re experiencing is that “Time OUT!” gets printed immediately, one time. It would be good to tell us what you’re currently experiencing and what you expect to happen, so that we can help. At work, our customer support staff often says things like “it doesn’t work”, and that actually makes it impossible to know what problem they’re experiencing in order to be able to help. It usually leads to a wild goose chase. – Nate Dec 24 '18 at 15:28
  • 2
    I'm sure you just wanted an answer that you could copy-paste and it would just work, but that's not the purpose of this site. If you're not willing to put in effort to understand what your problem is and a) communicate it clearly and b) actually learning what the solution is and why it works, it's true that you're probably better off somewhere else. – JJJ Dec 24 '18 at 18:34

1 Answers1

2

Problem is you are not actually changing t how you think. Really you are setting "virtually infinite" timeouts asynchronously (as it will loop over the while as fast as it can), so they will not block the execution every 500ms as you intend.

I added id # in the selector since that's how I could test it. If you want to do it your way decreasing the time from the initial by 1 per iteration:

function timer(t) {

    let mytime = setInterval(function() {
        t--;
        if(t<=0){
          alert("Time OUT!");
          clearInterval(mytime);
        }
    }, 500);
};
$("#playbutton").click(function() {
    var randomTime = Math.round(Math.random() * 100 + 20);
    timer(randomTime);
    alert(randomTime);
});

Asynchronous functions are a step change from what you usually start out learning with js but they are important to understand

wraig
  • 71
  • 4