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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

culpritCommented:
you're using javascript ?
0
arthrexAuthor Commented:
yes I do
0
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
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

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
WarpsehCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.