What's wrong with my code?

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 want to do is that when the condition time [5] === d happens, multiply that if those values are negative. But I just want it to multiply once. But we come across a problem as it is in setinterval, it is constantly multiplying, because time [5] stays for 4 seconds.

How can I do that just make me 1x?

Thank you.

    
asked by anonymous 24.07.2017 / 12:57

1 answer

4

You should clear the interval once your condition is satisfied.

This way:

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

So I believe, you just set clearInterval within your if(tempo[5] === "d") to run the routine just once.

Update:

Tested here worked without giving error in my browser, however I do not would use such a script. It is infinite loop. I did it because you asked me to. So to the staff that will screw up pie and think of downvotes. Know; I would never use this!

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);

Only one additional:

Remember that setTimeout will execute your routine after X milliseconds, in the case above 1000 ms that is equal to a second and will not run any more.

and setInterval executes a routine every X milliseconds in the case there also 1 second, so it does not even need that clearTimeout it will execute and stop and again it will be invoked by setInterval .

    
24.07.2017 / 13:08