Difference between revisions of "Bot Playground/Built-in Functions/localStorage.on"
From SmartBots Developers Docs
Line 26: | Line 26: | ||
== Example == | == Example == | ||
+ | To see how localStorage.on("update") works you'll need two scripts: | ||
+ | |||
+ | The "Listener" script: | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
localStorage.on("update", function(entry, value, script) { | localStorage.on("update", function(entry, value, script) { | ||
console.log("localStorage got updated:", entry, "=", value, "by script", script); | console.log("localStorage got updated:", entry, "=", value, "by script", script); | ||
+ | |||
+ | exit(); | ||
}); | }); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | The "Initiator" script: | ||
+ | <syntaxhighlight lang="javascript"> | ||
+ | localStorage.set("data", "data from 1"); | ||
+ | exit(); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | # Launch the "Listener" script (it will keep running) | ||
+ | # Launch the "Initiator" script (it will run and stop) | ||
+ | # Now switch to the "Listener" script and check the runtime logs | ||
{{NavMenu}} | {{NavMenu}} |
Revision as of 22:02, 14 July 2018
Adds the event callback on localStorage. Also check localStorage.set().
localStorage.on(eventName, callback);
Reference
This command accepts the following parameters:
Variable | Required | Description
| |
---|---|---|---|
Input: | |||
eventName | yes | the name of the event. Currently only supported event is "update" | |
callback | yes | the callback function to fire on event: function(entry, value, script) where
|
Comments
Use this function if you want to know when other scripts do localStorage.set().
Important: the "update" event does not react on localStorage.set() calls in current script.
Example
To see how localStorage.on("update") works you'll need two scripts:
The "Listener" script:
localStorage.on("update", function(entry, value, script) {
console.log("localStorage got updated:", entry, "=", value, "by script", script);
exit();
});
The "Initiator" script:
localStorage.set("data", "data from 1");
exit();
- Launch the "Listener" script (it will keep running)
- Launch the "Initiator" script (it will run and stop)
- Now switch to the "Listener" script and check the runtime logs