Usually we call a Javascript function, when a button is click.
<button onclick=function1();>Post message</button>
Or sometimes, we may call a Javascript function at a particular time.
window.onload = myFunction();
function myFunction()
{
setInterval(function1,t);
}
where function1() is any Javascript function. I want to call this function only if value of a variable changes in the script.
For instance: Lets say I have a variable abc. Now value of that variable changes when I select something on the page using the function below.
var abc = function getSelectionHTML() {
var userSelection;
if (window.getSelection) {
// W3C Ranges
userSelection = window.getSelection ();
// Get the range:
if (userSelection.getRangeAt) {
if (userSelection.rangeCount > 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 '';
}
}
How to alert(abc) only when the value of variable changes? Please help!