Observe variable >> need to get "on change event"

Hi experts,

I have a global variable which is false.
I need to know when it changes to true.
Please no solution with while-loop.

Thanks a lot!
arthrexAsked:
Who is Participating?
 
WarpsehConnect With a Mentor Commented:
Would something like this be useful?
function Observer(val)
{
	this.value = val;
	this.setValue = function(newValue)
	{
		this.value = newValue;
		this.CallObservers();
	};
	this.getValue = function()
	{
		return this.value;
	};
	this.Observers = new Array();
	this.CallObservers = function()
	{
		if (this.Observers instanceof Array)
		{
			for (var i = 0; i < this.Observers.length; i++)
			{
				this.Observers[i]();
			}
		}
	};
	this.AddObserver = function(observer)
	{
		this.Observers[this.Observers.length] = observer;
	}
}
test = new Observer("value1");
test.AddObserver(test2);
test.setValue("value2!!");
function test2()
{
	alert("Changed!");
}

Open in new window

0
 
culpritCommented:
you're using javascript ?
0
 
arthrexAuthor Commented:
yes I do
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
contactkarthiCommented:
use setTimeout to call a function every N seconds. in that function check the values of the boolean variable. if it changes show an alert

http://www.techiwarehouse.com/cms/engine.php?page_id=3e7a2280

have a look at the above url for example
0
 
arthrexAuthor Commented:
thanks a lot.
but I'm more searching for something like event.observe(element,"change",function(){...});
the problem is that in DOM2 there is no eventname "change" for a variable, just for form elements.
0
 
hieloCommented:
You need the watch method, but is not cross- browser compatible (as far as I know):
http://www.devguru.com/technologies/ecmascript/quickref/watch.html
0
 
arthrexAuthor Commented:
thanks for the response. well I need a cross-browser solution so the Observer part looks in general fine but not for my requirement, cause class A sets the value on its own

class A {
public boolean watchme = false;
}

class B {
// if A.watchme changes to true then dosomething
}
0
 
arthrexAuthor Commented:
Thank you all very much  for your responses.
At the end I used Warpseh's solution slightly modified.
0
All Courses

From novice to tech pro — start learning today.