What’s wrong with my code?

Asked

Viewed 88 times

1

var quantidade = "200";

setInterval(function(){
    var tempo     = document.getElementById("banner").textContent; 
    var match     = tempo.match(/[^\d](\d+)/);
    var nr        = match && match[1];
    if(nr === "5"){
        var red   = document.getElementById("red").textContent;
        var green = document.getElementById("green").textContent;
        var black = document.getElementById("black").textContent;
        if(red>black){
            var corfinal = "black";
            $("#enviarpreto").click();
        }else{
          var corfinal = "red";
          $("#enviarvermelho").click();
        }
    }

   if(tempo[5] === "d"){
       var red2     = document.getElementById("red").textContent;
       var green2   = document.getElementById("green").textContent;
       var black2   = document.getElementById("black").textContent;
       var meured   = document.getElementById("meured").textContent;
       var meublack = document.getElementById("meublack").textContent;
       if(meured < 0){
           quantidade = quantidade*2;
       }else if(meupreto < 0){
           quantidade = quantidade*2
       }else{
           quantidade = 200;
       }
       document.getElementById('betAmount').value = quantidade;
   }
}, 1000);

What I intend to do is that when the time condition[5] === d happens, it will multiply if those values are negative. But I just want you to multiply it once. But we have a problem as it is in setinterval, it is constantly multiplying, because the time[5], is for 4 seconds.

How can I so that it only makes me 1x?

Thank you.

  • 1

    A hint, indenting the code helps to find any errors in it. It won’t necessarily make you find the mistake, but it will reduce the effort needed to get there.

  • Thanks for the tip Renan!

1 answer

4

You should clear the interval as soon as your condition is satisfied.

Thus:

var myVar = setInterval(function(){ 
                  // definir condição.
                  if(n == 5){
                    // limpe o interval para parar a execução.  
                    clearInterval(myVar);
                  }       
            }, 1000);

So I believe, just define the clearInterval within your if(tempo[5] === "d") to perform the routine only once.

Updating :

Tested here worked without giving error in my browser, however I do not I would use a script like this. It’s infinite loop. I did it because you asked me to. So the people who are going to face the wrong way and think about downvotes. Know; I would never use that!

CallTimeOut = function(){
       var myTimeOut = setTimeout(function(){
       // execute aqui sua rotina 
       //Aqui embaixo sua condição para encerrar o TimeOut  if(tempo[5] === "d")
       if(1==1) clearTimeout(myTimeOut);
   },1000);
}

setInterval(function(){
    if(typeof(myTimeOut) == 'undefined'){
        CallTimeOut(); 
    }   
},1000);

Just an additional:

remember that setTimeout will perform your routine after X milliseconds, in the case above 1000 ms which is equal to a second and will not perform more.

and setInterval, performs a routine every X milliseconds in the case there also 1 second, so it doesn’t even need that clearTimeout it will run and stop and again will be invoked by setInterval.

  • This is working the first time, but then I need the set interval back in. In other words, all I have to do is give me 1 x, then I wait for the time it gets active and the setinterval gets back. And this condition is completely ending me with setinterval

  • Explain to me better, what does it do? I didn’t understand your comment. Why do you need to keep running this second-to-second check?

  • Because time is a chronometer, and I have other conditions within that setinterval

  • If there is any way, after the clear interval, to re-activate the setinterval was the ideial

  • I don’t know if I got it right, but I think you could use setTimeout where you put setInterval, and put inside the scope of a function. Then define a setInterval in your overall script scope with an end-of-execution condition of setTimeout by invoking the function again. This would give the functionality you want.

  • Could you give me an example?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.