title: How to call a Javascript function when value of a variable changes?
body: <p>Usually we call a Javascript function, when a button is click.   </p>
  
  <pre><code>&lt;button onclick=function1();&gt;Post message&lt;/button&gt;
  </code></pre>
  
  <p>Or sometimes, we may call a Javascript function at a particular time.</p>
  
  <pre><code>window.onload = myFunction();
            function myFunction()
             {
                   setInterval(function1,t);  
             }
  </code></pre>
  
  <p>where <code>function1()</code> is any Javascript function. I want to call this function only if value of a variable changes in the script.</p>
  
  <p>For instance: Lets say I have a variable <strong>abc</strong>. Now value of that variable changes when I select something on the page using the function below. </p>
  
  <pre><code>var abc = function getSelectionHTML() {
        var userSelection;
        if (window.getSelection) {
          // W3C Ranges
          userSelection = window.getSelection ();
          // Get the range:
          if (userSelection.getRangeAt) {
            if (userSelection.rangeCount &gt; 0)
          var range = userSelection.getRangeAt(0);
            else
          return '';
          } else {
            var range = document.createRange ();
            range.setStart (userSelection.anchorNode, userSelection.anchorOffset);
            range.setEnd (userSelection.focusNode, userSelection.focusOffset);
          }
          // And the HTML:
          var clonedSelection = range.cloneContents ();
          var div = document.createElement ('div');
  
          div.appendChild (clonedSelection);
          return div.innerHTML;
        } else if (document.selection) {
          // Explorer selection, return the HTML
          try {
            userSelection = document.selection.createRange ();
            return userSelection.htmlText;
          } catch (err) {
            return '';
          }
        } else {
          return '';
        }
      }
  </code></pre>
  
  <p><strong>How to <code>alert(abc)</code> only when the value of variable changes?</strong> Please help! </p>