code flow not making sense

code flow not making sense

The code flow is not making sense. The "on keyup" event for a text box executes an ajax post to a php page. If a match is found, the php page returns {"duplicate":"Yes"} and the jquery executes the code in the code block for if(msg.duplicate == 'Yes'){ ... } and plays a sound. If a match is not found the php page returns {"duplicate":"No"} and a sound is not played.


No sound is played until msg.duplicate == 'Yes' for the first time, which is good. What's bad is, once the sound is played for the first time, and I then change the input in the text box so {"duplicate":"No"} is retuned, the sound is still played even though the $('#matchAudio')[0].play() command is only inside the if(msg.duplicate == 'Yes'){ ... }  code block.


How is code inside the if(msg.duplicate == 'Yes'){ ... }  code block being executed if the program flow is inside the if(msg.duplicate == 'No'){ ... }  code block?


I tried putting a $('#matchAudio')[0].pause() command in the if(msg.duplicate == 'No'){ ... }  code block, but that still did not prevent the sound from playing after the first time the sound was played.


  1. $(document).ready(function(){
     $('<audio id="matchAudio"><source src="notify.ogg" type="audio/ogg"><source src="notify.mp3" type="audio/mpeg"></audio>').appendTo('body');
     
     $lkpchkbxs_m.on('keyup', lookup_check);
     
     function lookup_check() {
      var jqXHR = null;
      var lkpchkbxs = $lkpchkbxs_m.val();
      var lkpchkbxtype = $lkpchkbxtype_m.filter(':checked').val();
      //var lkpchkbxtype = $("input:radio[name=lookupchkbxs_option]:checked").val();
      var label_l = "<label>Live Search - Full:</label>";
      if ($.trim(lkpchkbxs) != ''){
       console.log("lookup: " + lkpchkbxs + " | " + lkpchkbxtype);
       if(jqXHR != null){
        jqXHR.abort();
       }
       jqXHR = $.ajax({
        type: "POST",
        url: "check.php",
        data: {"lkpchkbxs" : lkpchkbxs, "lkpchkbxtype" : lkpchkbxtype, "chkbxs_m" : "lookup"},
        dataType: "JSON",
        success: function(msg) {
         $(document).ajaxComplete(function(event,request,settings){
          if(msg.duplicate == 'Yes'){
           $status_l.html (label_l + '<img src="check.png">Search Finished.');
           $result_l.html(''); 
           $result_l.append('<img src="arrow.png"><b>' + msg.n_date);
           $result_l.append(':</b> ');
           $result_l.append(msg.search_string);
           $('#matchAudio')[0].play();
          } else if(msg.duplicate == 'No') {
           $status_l.html (label_l + '<img src="loader.gif">Searching...');
           $result_l.html('');
           if(msg.length == 6 && msg.num_boxes == "49"){
            $status_l.html (label_l + '<img src="check.png">Search Finished.');
            $result_l.append('<img src="close.png">Match not found.');
           }
           if(msg.length == 5 && msg.num_boxes == "39"){
            $status_l.html (label_l + '<img src="check.png">Search Finished.');
            $result_l.append('<img src="close.png">Match not found.');
           }
          }
         }); 
        }
       });
      } else {
       $status_l.html ('');
       $result_l.html(''); 
      }
     }
    });