DEV Community

Cover image for How to make a Variable Observer
Mafee7
Mafee7

Posted on

3 3

How to make a Variable Observer

There are many Deprecated Methods to listen for changes in a variable's value in Javascript

Here is a easy way to listen for changes in a variable's value:

var Var = window.innerWidth + ", " + window.innerHeight; // Can Put Anything Here as The Value.
var oldVar = Var; // Set oldVar to Var.

setInterval(function(){
    // Set Var to Value;
    Var = window.innerWidth + ", " + window.innerHeight; 
},100)

setInterval(function(){
    if(oldVar !== Var){ // Check If Var Not = oldVar.
        // Do Something.
        console.log("Window Resized!");
        oldVar = Var; // Set oldVar to Var to reset the code.
    }
}, 100);

Enter fullscreen mode Exit fullscreen mode

Or Directly Use Value:

var oldVal;


setInterval(function(){
    if(oldVar !== window.innerHeight){ // Check If Valuez Not = oldVar.
        // Do Something.
        console.log("Window Resized!");
        oldVar = window.innerHeight; // Set oldVar to Value to reset the code.
    }
}, 100);

Enter fullscreen mode Exit fullscreen mode

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay