Checking While Condition and Inner setInterval Condition

23 Views Asked by At

var conmath = "1000 - {{sum}}";
var re = /{{([^{}]+)}}/;
var m;
console.log('Conmath-Outside While: ' + conmath);
while (m == re.exec(conmath)) {
  console.log('M: ' + m);
  var mfld = m[1];
  console.log('MFld: ' + mfld);
  console.log('Conmath-Inside While: ' + conmath);
  var mult = "2";
  var mfldi = "field~~sum"
  var mfldtxt = '#field' + '\\~\\~' + mfld;
  console.log('MfldTxt ' + mfldtxt);
  waitmfld = $(mfldtxt).filter(function() {return $(this).attr('data-mult') == mult}); // set var global to use in other funcs
  console.log('Waitmfld: ', $(waitmfld));
  var wtime = 0;
  return; // temp - comment out to see infinite loop
  var waitForElement = setInterval(function(waitmfld, mfld) {
    console.log('Inside Wait');
    console.log('ClassCk: ' + $(waitmfld).hasClass('done'));
    if (wtime > 1500) { // stop wait after 2secs
      console.log('Ending Wait: ' , $(wait));
      clearInterval(waitForElement);
    }  
    if ($(waitmfld).hasClass('done')) {
      console.log('Wait Has Class');
      var mval = $(waitmfld).text();
      console.log('Mval-Wait: ' + mval); // should be 3
      var conmath = "1000 - 3"; // set conmath to check if can get out of while loop
      clearInterval(waitForElement);
    }
    wtime += 500;
  }, 500);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

<span id="field~~sum" class="reg" data-mult="1">something</span>
<span id="field~~sum" class="reg done" data-mult="2">3</span>

I am trying to update a variable (conmath) until it doesn't contain any more {{var}} variables. I'm using while loop and a setInterval inside of that.

Here's my fiddle: https://jsfiddle.net/billparti/oxej2s91/

    var conmath = "1000 - {{sum}}";
    var re = /{{([^{}]+)}}/;
    var m;
    console.log('Conmath-Outside While: ' + conmath);
    while (m == re.exec(conmath)) {
      var mfld = m[1];
      console.log('MFld: ' + mfld);
      console.log('Conmath-Inside While: ' + conmath);
      var mult = "2";
      var mfldi = "field~~sum"
      var mfldtxt = '#field' + '\\~\\~' + mfld;
      console.log('MfldTxt ' + mfldtxt);
      waitmfld = $(mfldtxt).filter(function() {return $(this).attr('data-mult') == mult}); // set var global to use in other funcs
      console.log('Waitmfld: ', $(waitmfld));
      var wtime = 0;
      return; // temp - comment out to see infinite loop
      var waitForElement = setInterval(function(waitmfld, mfld) {
        console.log('Inside Wait');
        console.log('ClassCk: ' + $(waitmfld).hasClass('done'));
        if (wtime > 1500) { // stop wait after 2secs
          console.log('Ending Wait: ' , $(wait));
          clearInterval(waitForElement);
        }  
        if ($(waitmfld).hasClass('done')) {
          console.log('Wait Has Class');
          var mval = $(waitmfld).text();
          console.log('Mval-Wait: ' + mval); // should be 3
          conmath = "1000 - 3"; // set conmath to check if can get out of while loop
          clearInterval(waitForElement);
        }
        wtime += 500;
      }, 500);
    }

Note: I put a temp return before the setInterval so you can see if there are other errors and it also prevents an infinite loop from happening.

0

There are 0 best solutions below