Stop settimeOut Javascript

1

I'm doing a function to check a certain iteration and if it goes from x I pause the contactor, but it is not working, it continues to pop up 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
    
asked by anonymous 29.10.2015 / 14:07

1 answer

0

Probably the problem is that% as_% is declared inside time .

This means that every time a new instance is created it is called verificaComplet within verificaComplet .

Perhaps what solves the problem is to "declare on the outside" of the function so that this variable becomes the sole 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>

Notice that despite setTimeout in there, my function only wrote 1 time, because setTimeout was cleaned.

    
14.12.2015 / 16:38