The event namespace of Bijou.js, containing functions to listen and dispatch events, such as scroll stop, outside click, and multiple event listeners.
Methods
# static exports.dispatch(type, args, targetopt) → {Event}
Dispatches an event of the type specified with custom arguments.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
type |
String
|
The type of event to dispatch (E.g. "mousemove") |
||
args |
Object
|
The argument representing the event, e.g. {clientX: 100, clientY: 150} |
||
target |
EventTarget
|
<optional> |
window | What to dispatch the event to. |
The event object.
Event
Example
//Dispatch a custom mouse move event to the window.
_$.dispatch("mousemove", {clientX: 100, clientY: 150, target: document.documentElement}, window);
# static exports.hub() → {Object}
A lot like socket.io, this allows emit, on and off handlers. (Note that this is local, only your computer sends and recieves your data. Still useful though)
The object with the emit, on and off functions in it.
Object
Example
let thing = _$.hub();
// Log any new data to the console
thing.on("data", (data) => console.log(data));
setTimeout(() => {
thing.emit("data", "Yay! Some data!!"); // Logs "Yay! Some data!!" to the console after 2 seconds.
}, 2000)
# static exports.onOutsideClick(element, callback) → {Promise}
Returns the callback when a a click is registered outside the selected element
Parameters:
| Name | Type | Description |
|---|---|---|
element |
Element
|
The element to use as the outsideclick element. |
callback |
function
|
The function to run when a click is registered outside the specified element. |
A promise that is resolved when the user clicks outside the specified element.
Promise
Example
_$.onOutsideClick(document.querySelector("div"), () => {alert("You clicked outside the DIV!")});
# static exports.onScrollStop(elementopt, callback, timeopt) → {Promise}
Returns the callback when the user stops scrolling.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
element |
HTMLElement
|
<optional> |
window | The HTML element to listen on for scroll stop. |
callback |
function
|
The callback to call when the user stops scrolling. |
||
time |
Number
|
<optional> |
150 |
Returns a promise that is resolved when the user stops scrolling.
Promise
Example
_$.onScrollStop(() => {alert("You stopped scrolling!")})
# static exports.waitUntil(condition, waitopt) → {Promise}
Waits until a condition is met then resolves a promise.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
condition |
function
|
The function which returns true when the condition is met |
||
wait |
Number
|
<optional> |
Infinity | The wait time in milliseconds to cancel the function and reject the promise. |
A promise resolved when the condition returned by the function is true.
Promise
Examples
//Waits until the current second of the current minute is 10.
_$.waitUntil(() => new Date().getSeconds === 10).then(() => console.log("Done"))
//This DOES NOT work
_$.waitUntil(() => Date.now() === Date.now() + 100);
//Because it is evaluated many times, and the current date, is never ahead of itself. Therefore in this case the function will run infinitely.
//To fix this problem and cancel the function after a certain amount of time,
//you can pass another argument to the function
_$.waitUntil(() => false, 10000);//Waits 10 seconds, because the function always returns false.