Video and Audio keeps restarting when condition is not met
<p><strong><em>Functionality:</em></strong></p>

<p>I have made use of Arduino to sense a user proximity. And when proximity is sensed, arduino will detect and return a "1". There is a delay of 4 seconds: meaning a return  of 4 "1"s before, the video that is playing will restart and accompanied with the video's audio.</p>

<p>When user moves away, arduino will detect and return a "0". There will be a delay of 3 seconds: meaning 3 "0"s before the video's audio is mute, while the video will continue to play. Meanwhile, for added user experience, even when user is there but happens to fidget such that the arduino detects and returns a "0", the video with audio is suppose to continue without running the function of stop Audio, unless there is a return of 3 "0"s.</p>

<p><strong><em>Issue:</em></strong></p>

<p>I have managed to achieve the following:</p>

<p>1.) video replays with audio when arduino returns 4 "1"s
2.) audio is mute but video will continue to play when arduino returns 3 "0"s</p>

<p>However this is the issue that I am currently facing:</p>

<p>1.) I am not able to get video with audio to continue playing when arduino returns at least a "0" or 2 "0"s before the condition of 3 "0"s. The video with audio will be interrupted and when user is still within the proximity, video will restart with audio.</p>

<p>This is no the behaviour that i am looking for.
Correct behaviour flow:</p>

<ul>
<li>User proximity sensed: arduino returns 4 "1"s, video will restart with audio. When user fidgets, arduino returns a temporary "0", video with audio will still continue to play. however, when user leaves, arduino returns 3 "0"s, audio will mute while video will continue to play</li>
</ul>

<p>Code:</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>function getFeedback() {
  //To call on ajac getFeedBack
  ajax_getArduinoFeedback("flash.do", "formType=getArduinoFeedback");
}

//To stop interrupt method and reset fields to original
function stop_interrupt() {
  isInterrupt = false;
  triggerFeedback = "0";

  setTimeout(function() {
    $("#M_Video").jPlayer("mute", true);
  }, 3000);
  //console.log("stop_interrupt triggerFeedback: " + triggerFeedback );          
}

//Interrupt method to call on refresh method
function show_interrupt(flag) {
  //Set Timeout for flag to be equal to "1"
  isInterrupt = true;
  isPlay = false;
  triggerFeedback = flag;

  if (flag == "1") {

    Flag = setTimeout(function() {


      if (triggerFeedback == "1") {
        //Display digital alert

        console.log("4 seconds later");
        console.log("flag: " + triggerFeedback);

        //Clear Timeout value of flag="1"  when current flag
        clearTimeout(Flag);

        //Clear Timeout value of flag ="0"
        clearTimeout(Absent);

        if (isPlay == false) {
          isPlay = true;
          $('#smell_background').fadeIn({
            duration: slideDuration,
            queue: false,
            complete: function() {

              $('#smell_text').fadeIn({
                duration: slideDuration,
                queue: false
              });
              //Smell-Image to fadeOut after delay
              setTimeout(function() {
                $('#smell_text').fadeOut({
                  duration: slideDuration,
                  queue: false
                });
                setTimeout(function() {
                  $('#smell_background').fadeOut({
                    duration: slideDuration,
                    queue: false
                  });
                }, 900);
              }, 4000);
            }
          });

          setTimeout(function() {
            //Restart video

            $("#M_Video").hide();

            $("#M_Video").jPlayer("setMedia", {
              m4v: "lib/video/video_03.mp4"
            }).jPlayer("play");
            $("#M_Video").jPlayer("mute", false);

            setTimeout(function() {
              $("#M_Video").fadeIn();
            }, 2500);
            console.log("videoList:" + videoList[videoIndex]);
          }, 5000);
        }
      }
    }, 2000);
  } else if (flag == "0") {

    isPlay = false;
    clearTimeout(Flag);
    console.log(isPlay + "1");

    Absent = setTimeout(function() {
      if (triggerFeedback == "0") {
        clearTimeout(Flag);
        isPlay = false;
        console.log("flag: " + triggerFeedback);

        console.log(isPlay + "2");
        if (isPlay == false) {
          console.log(isPlay + "3");
          stop_interrupt();
        }
      }
    }, 5000);

  }
}

//Method call when data from Arduino is "1"
function interrupt(flag) {
  console.log("isInterrupt : " + flag);
  //location.reload();
  if (isInterrupt == false) {

    if (triggerFeedback != flag) {
      if (triggerFeedback == "1" &amp;&amp; flag == "0") {
        //stop_interrupt();
        show_interrupt(flag);
      } else {
        if (flag != "0") {
          //console.log("1");
          show_interrupt(flag);
        }
      }
    } else {
      //console.log("2");
      show_interrupt(flag);
    }
  } else {
    if (triggerFeedback != flag) {
      if (triggerFeedback == "1" &amp;&amp; flag == "0") {
        //stop_interrupt();
        show_interrupt(flag);
      } else {
        if (flag != "0") {
          //console.log("3");
          show_interrupt(flag);
        }
      }
    }
  }
}</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;body oncontextmenu="return false"&gt;

  &lt;div id="M_Start" align="center" class="M"&gt;

    &lt;!--Video Div--&gt;
    &lt;div id="M_Video" style="position:absolute; z-index:99; left:-80px; top:0px;"&gt;&lt;/div&gt;
    &lt;!--Alert background--&gt;
    &lt;img id="smell_background" class="Smell_Cask" src="lib/img/Smell_Background.png" /&gt;

    &lt;!--Alert Text--&gt;
    &lt;img id="smell_text" class="Smell_Cask_Text" src="lib/img/Smell_Text.png" /&gt;

  &lt;/div&gt;

&lt;/body&gt;</code></pre>
</div>
</div>
</p>

<p>//Arduino Code:</p>

<pre><code>const int trigPin = 8;
const int echoPin = 9; 
//Motor-Fan connected to arduino pin number
const  int FanPin = 5;

long duration;
int distance;

void setup() 
{
pinMode (FanPin , OUTPUT) ; // Set pinMode for FanPin as OUTPUT, display  
pinMode(trigPin, OUTPUT); 
pinMode(echoPin, INPUT);
Serial.begin(9600); // Open serial port to communicate with the Ultrasaonic Sensor
}

void loop() 
{

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance= duration*0.034/2;

if(distance &lt; 20)
{
  //delay a time of 2 seconds  before writing Print of '1' and set Motor fan to HIGH
  delay(1000);

  // Write a pin of HIGH
  Serial.println("1");
  //Set motor-fan to operate
  digitalWrite (FanPin, HIGH);
}
else
{
  Serial.println("0");
  //Set motor-fan to stop
  digitalWrite (FanPin, LOW);
}
delay(100);
}
</code></pre>

<p>What have I done wrong or missing? Please help.</p>