1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
<script type="text/javascript">
o={myVar:'toto'}
function changePropertieValue(propertie,oldValue,newValue)
{
document.getElementById('aTextField').value=newValue;
}
function Watcher(object,propertie,handler)
{
this.object=object;
this.propertie=propertie;
this.handler=handler;
this.oldValue=object[propertie];
this.timer=null;
}
Watcher.prototype.start=function(){this.timer=setInterval(run(this),1000);}
Watcher.prototype.stop=function() {clearInterval(this.timer);}
function run(watcher)
{
return function()
{
if (watcher.object[watcher.propertie]!=watcher.oldValue)
{
watcher.handler(watcher.propertie,watcher.oldValue,watcher.object[watcher.propertie]);
watcher.oldValue=watcher.object[watcher.propertie];
}
};
}
watcher=new Watcher(o,'myVar',changePropertieValue);
watcher.start();
</script>
<input id="aTextField" type="text" value="toto" />
<input type="button" value="changer pour 'titi'" onclick="o.myVar='titi';"> |
Partager