execute after some time a function based on global value

waiting until a variable has a specific value and then you can execute it

by heriberto perez

HTML

<div style='font-size: 22px;' id='container'></div>
<!-- esto no funciona por qeu en jsfiddle no funciona bien las window.variable y por eso pero si lo corres por ejemplo en tu navegador en tu pagin avas a ver qeu funciona chulada y aqui si tienes qeu esperar hasta que termine no puedes hacer window.Value=true desde consola por qeu no lo toma en cuenta-->

JavaScript

jQuery(function(){
  var n = 100;
  
  var val = 'test_params';
  setTimeout(function(){countDown(val)}, 1000);

  function countDown(val){
    n--;
    console.log(val);
    if(window.Value) {
      n = 0;
      console.log('execute this finally');
    }
    if(n > 0){
      setTimeout(function(){countDown(val)}, 2000);
    }
    
    jQuery('#container').html(n);
  }
});