Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/gigabyte5671/variablelistener

A library enabling JavaScript to execute arbitrary functions when the value of any global variable changes.
https://github.com/gigabyte5671/variablelistener

javascript javascript-library listener variables

Last synced: 5 days ago
JSON representation

A library enabling JavaScript to execute arbitrary functions when the value of any global variable changes.

Awesome Lists containing this project

README

        

# Variable Listener

Allows JavaScript to execute arbitrary functions when the value of any global variable changes.

![npm bundle size](https://img.shields.io/bundlephobia/min/variable-listener)

## Installation

To add Variable Listener to your site or project:
```html

```

or

```bash
npm install variable-listener
```

```javascript
import 'variable-listener';
```

## Usage

Declare your global variable:
```javascript
var count = 10;
```

Add a new listener:
``` javascript
window.addVariableListener('count', function (newValue) {

console.log(newValue);

});
```

The callback will be triggered whenever your variable changes:
```javascript
count++;

// Callback logs: 11
```

If you no longer want to listen to the variable, call:
``` javascript
window.removeVariableListener('count');
```