https://github.com/loadingio/polling
tiny vanilla JS Polling library
https://github.com/loadingio/polling
Last synced: 18 days ago
JSON representation
tiny vanilla JS Polling library
- Host: GitHub
- URL: https://github.com/loadingio/polling
- Owner: loadingio
- License: mit
- Created: 2019-07-21T07:29:04.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-07-19T23:51:50.000Z (over 2 years ago)
- Last Synced: 2025-08-08T19:05:42.713Z (6 months ago)
- Language: LiveScript
- Size: 113 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @loadingio/polling
Promise based, vanilla JS polling library. Features:
- Implemented via setTimeout
- Polling paused when page is not visible ( checked with page visibility api )
- Promise based ( resolved when polling ends. )
- Object-aware, can be used in object member function.
## Usage
install via npm:
npm install --save @loadingio/polling
or include directly the dist file:
To periodically invoke a function with certain interval(in millisecond): `polling(function, interval)()` or `polling(interval, function)()`
example:
poll = polling(function() { console.log("print every 2 seconds."); }, 2000)
poll();
Return true in your function to stop the polling, which will be used to resolve the pending promise:
polling(function() {
if(Math.random() > 0.9) { return "hit!"; }
}, 1000)()
.then(function(msg) {
console.log("message: ", msg);
})
You can also force it to stop by calling "stop" on the returned object directly:
var poll = polling(f, 1000);
poll();
poll.stop();
It also works as a member function of object:
var myobj = {
poll: polling(function() {
if(this.data == true) {
return true;
} else {
console.log("polling");
}
return false;
},
data: false
};
myobj.poll();
## License
MIT