0

Functionality:

The secondary function is called when the initial condition is detected and satisfied. Therefore if '(condition = true)', the secondary function will be called. However, the secondary function should only be called once until the condition is changed to false.

Hence, the correct behaviour is that: The secondary function shouldn't be kept calling when the initial condition is still 'true', the correct behaviour is that it should only be called once when the 'condition =true' and when 'condition=false', nothing happens and the next instance when 'condition=true' again, the secondary function will be called again once, untill the condition = false.

At this point in time, what I have done is that at every 'condition= true', it will be always be calling the secondary function=>location.reload();, which happpens to be the wrong behaviour. Therefore, if the 'condition=true' were to be true for 10 seconds, it will be refreshing every second for the 10 seconds.

Hence, at this point, I am stuck at how to only call 'location.reload()' only once when 'condition= true', such that it will only refresh the page once.

Therefore, I need help/guidance on how to only detect once and call function once when 'condition=true'

 var isInterrupt =false;
//Interval to keep asking the backend for sensor value: Hence, if sensor value returns a '1', it will call the function interrupt()
    setInterval(getFeedback,100);

    function getFeedback()
    {
        ajax_getArduinoFeedback("flash.do", "formType=getArduinoFeedback");
    }

    //Method call when data from Arduino is "1"
    function interrupt(){

        //location.reload();

        if (isInterrupt == false)
        {
            isInterrupt = true;
            //When data="1" at the arduino board, Video will reload and start playing

            location.reload();

        }
        isInterrupt=false;
    }
Luke
  • 982
  • 1
  • 7
  • 27

1 Answers1

0

You must keep the record of isInterrupt in session, a cookie or localStorage.

At the beginning of your script it would go like this:

var isInterrupt = localStorage.getItem("isInterrupt"); /* false by default */

and once you change the value in script, also change it in localStorage:

localStorage.setItem("isInterrupt", "anything");

or just remove/unset it:

localStorage.removeItem("isInterrupt");

So, the complete example would go this way:

var savedInterrupt = localStorage.getItem("savedInterrupt");
setInterval(getFeedback,100);

function getFeedback() {
    ajax_getArduinoFeedback("flash.do", "formType=getArduinoFeedback");
}

function interrupt(){
    if( isInterrupt != savedInterrupt ) {
        localStorage.setItem("savedInterrupt", isInterrupt);
        location.reload();
    }
}

Depending on needs, maybe you need to use the sessionStorage, which last for only one session. Methods are same for both.

While data stored in localStorage has no expiration set, data stored in sessionStorage gets cleared when the page session ends

skobaljic
  • 9,379
  • 1
  • 25
  • 51
  • Do I need to pass the method in??And then for this do I still need to declare isInterrupt = true in condtion statement? – Luke Apr 09 '16 at 11:58
  • I am a lil confused, but where do you set : `localStorage.setItem("isInterrupt", "anything");` and `localStorage.removeItem("isInterrupt");` – Luke Apr 09 '16 at 12:00
  • I do not see where you set `isInterrupt` back to `false`? Than I could create you complete example. – skobaljic Apr 09 '16 at 12:01
  • my initial isInterrupt is declared as false. is Interrupt is set tofalse outside of the if statement – Luke Apr 09 '16 at 12:03
  • ok, allow me to test it out and understand and digest the code. – Luke Apr 09 '16 at 12:47
  • would just like to ask is that: for localStorage is either we use `localStorage.setItem("isInterrupt", "anything");` or `localStorage.removeItem("isInterrupt");`, can't really use both?? So for `localStorage.setItem("isInterrupt", "anything");`, is that at every new value, it will replace the existing value? – Luke Apr 10 '16 at 01:40
  • And Secondly, what I understand from the following code is that if `isInterrupt = true`, it will set into the localStorage and do the location.reload() method and when condition is changed `isInterrupt =false`, it will also set into the localStorage, but I would like to confirm will it also do the location.reload() method when the condition is changed to false? – Luke Apr 10 '16 at 01:51
  • The idea is to set last condition in localStorage, no matter what the value is. Next time we have new condition, than we reload the page. `removeItem` will just unset the item from localStorage and will return false next time you try to get it. I've placed it there just for information and for testing you will need it to reset the browser state back to beginning. – skobaljic Apr 10 '16 at 03:00
  • would just like to check with you, is it a must to removeItem everytime a value has been set??then, is it correct if I put the removeItem outside the if condition statement? – Luke Apr 10 '16 at 04:52
  • Secondly, in my console log,the isInterrupt is always null though – Luke Apr 10 '16 at 04:59
  • Try to forget about removeItem, you may need it only for testing. About isInterupt was not set - make sure it was defined in global scope (for example set `var isInterupt;` at beginning of your script. More about [variable scope](https://msdn.microsoft.com/library/bzt2dkta%28v=vs.94%29.aspx) in Javascript you can find [here](http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript). – skobaljic Apr 10 '16 at 12:32