Because it detects class that are erased

0

I'm trying to make a jquery script that detects one class inside another and when it detects it notify, delete it and continue running until it detects another one.

But the problem is that it detects a class that is deleted before and is not in the DOM.

link

$(document).ready(function() {
var audio = new Audio('https://www.myinstants.com/media/sounds/sound-9______.mp3');
var $myDiv = $('.foo .biz');
var divss = 0;
function detect_drop(){
    if ( $myDiv.length) {
   			 divss++;
        console.log(divss);  
        audio.play();
        $( $myDiv ).remove();
        
    }
}
setInterval(detect_drop, 10000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="foo">
  <div class="bar"></div>
  <div class="bar">
<div class="biz"></div>
  </div>
  <span></span>
</div>
    
asked by Kaiserdj 22.08.2017 в 14:16
source

1 answer

3

Although the div is no longer in the DOM, the variable $ myDiv has not changed and still contains the elements that were in its time.

One option would be to calculate $ myDiv in each run like this:

$(document).ready(function() {
  var audio = new Audio('https://www.myinstants.com/media/sounds/sound-9______.mp3');
  var divss = 0;
  function detect_drop(){
    var $myDiv = $('.foo .biz');
    if ( $myDiv.length) {
          divss++;
          console.log(divss);  
          audio.play();
          $( $myDiv ).remove();   
      }
  }
  setInterval(detect_drop, 10000);
});
    
answered by 22.08.2017 / 15:16
source