lisovaccaro
lisovaccaro

Reputation: 33956

Javascript watch for variable value change?

Basically I have a function that reads cookies. getCookie('message').

When getCookie('message') value changes I want to display an alert with the new value.

I think the function can be treated as a variable. Thus the question's title if not please let me know so I correct it. How can I listen for a change in the value the function returns?

Upvotes: 0

Views: 923

Answers (2)

sjain
sjain

Reputation: 23344

Ok. So you want to read the value of cookie. That is somewhat easy like this -

To read out a cookie, call this function and pass the name of the cookie. Put the name in a variable. First check if this variable has a value (if the cookie does not exist the variable becomes null, which might upset the rest of your function), then do whatever is necessary.

var x = readCookie('ppkcookie1')
  if (x) 
   {
    alert("this is a change"+x)
   }

Cookie read.

Upvotes: -1

Christian Westman
Christian Westman

Reputation: 3005

basic polling example

var message = getCookie('message');
setInterval(function(){
    var m = getCookie('message');
    if(m != message) {
        message = m;
        alert(message);
    }
}, 1000);

Upvotes: 6

Related Questions