Stop settimeOut Javascript

Asked

Viewed 901 times

1

Well I am doing a function to check certain iteration and if it passes x I pause the contactor, however it is not working, it continues to appear Alert window, follow my example below?

function verificaComplet(tam, page){
var tentativas = 0,
    maxRange = tam + 5,
    time;
console.log("max Range " + maxRange);
console.log("iteracoes " + iteracoes);
iteracoes = iteracoes + 1;

if(maxRange > iteracoes){
    if(w_variavel == tam){
        activate_page(page);
        desbloqueiTela("#BloqueiaLogin");
    }else{
        time = setTimeout(function () { verificaComplet(tam, page);}, 5000);
    }
}else{
    clearTimeout(time);
    desbloqueiTela("#BloqueiaLogin");
    alert("Tempo de espera escedido, Por favor tente novamente mais tarde.");
}
}

The output of the program is as follows:

max Range 22 VM2450 main.js:608
iteracoes 17 VM2450 main.js:609
max Range 22 VM2450 main.js:608
iteracoes 18 VM2450 main.js:609
max Range 22 VM2450 main.js:608
iteracoes 19 VM2450 main.js:609
max Range 22 VM2450 main.js:608
iteracoes 20 VM2450 main.js:609
max Range 22 VM2450 main.js:608
iteracoes 21 VM2450 main.js:609
max Range 22 VM2450 main.js:608
iteracoes 22 VM2450 main.js:609
max Range 22 VM2450 main.js:608
iteracoes 23 VM2450 main.js:609
max Range 22 VM2450 main.js:608
iteracoes 24 VM2450 main.js:609
max Range 22 VM2450 main.js:608
iteracoes 25 VM2450 main.js:609
max Range 22 VM2450 main.js:608
iteracoes 26 VM2450 main.js:609
max Range 22 VM2450 main.js:608
iteracoes 27 VM2450 main.js:609
max Range 22 VM2450 main.js:608
iteracoes 28 VM2450 main.js:609
max Range 22 VM2450 main.js:608
iteracoes 29 VM2450 main.js:609
max Range 22 VM2450 main.js:608
iteracoes 30 VM2450 main.js:609
max Range 22 VM2450 main.js:608
iteracoes 31 VM2450 main.js:609
max Range 22 VM2450 main.js:608
iteracoes 32 VM2450 main.js:609
max Range 22 VM2450 main.js:608
iteracoes 33 VM2450 main.js:609
max Range 22 VM2450 main.js:608
iteracoes 34 VM2450 main.js:609
max Range 22 VM2450 main.js:608
iteracoes 35 VM2450 main.js:609
max Range 22 VM2450 main.js:608
iteracoes 36 VM2450 main.js:609
max Range 22 VM2450 main.js:608
iteracoes 37 VM2450 main.js:609
  • could post the method code desbloqueiTela? the only explanation I see for this loop, is the desbloqueiTela is calling the verificaComplet

  • you could add the following line in the function verificaComplet: console.log("caller " + (arguments.callee.caller.name || "desconhecido"));

  • P.S: if using strict mode use verificaComplet.caller.name || "desconhecido" in place of arguments.callee

  • Another possibility is some method to change the value of [ iterations ], which is an external scope variable. And there’s also the variable [ w_variable ] which is also external-scoped.

  • P.S.: I got it to work on this Fiddle: https://jsfiddle.net/kb7s8eeq/ And one more thing. The clearTimeout you call has no use. The [ time ] variable is declared in every call to the function. So when you call clearTimeout(time) the value of time is Undefined. And setTimeout runs only once. The setInterval is executed continuously. Consider the conditions for your function to act correctly: maxRange must be greater than iterations w_variable must be equal to tam Both conditions are evaluated as "false".

  • Mokyn will test here in production to see if it will work

  • Guys I managed to solve... The problem was because I had a speaker who made the call to the function, but now everything worked well.

Show 2 more comments

1 answer

1

Probably the problem lies in the fact that time be declared within verificaComplet.

That makes it, every time it’s called verificaComplet within the setTimeout, a new instance is created.

Maybe what solves the problem is to "declare outside" the function so that this variable becomes the unique reference for the current timeout.

Example:

$(function()
{
	uniqueTimeout = (function(){
			// preste atenção nesse escopo
		    var time = 0;

	         return function (func, delay) {
	              clearTimeout(time);
	              time = setTimeout(func, delay);
	         }
	    })();

	var escreve = function(){
	     $('body').append('<div>escreveu uma vez?</div>');
	}


	uniqueTimeout(escreve, 1000)
	uniqueTimeout(escreve, 1000)
	uniqueTimeout(escreve, 1000)
	uniqueTimeout(escreve, 1000)
	uniqueTimeout(escreve, 1000)
	uniqueTimeout(escreve, 1000)


})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Note that despite the setTimeout inside, my job only wrote once, because the setTimeout was cleaned up.

Browser other questions tagged

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