| online? | |
online?::boolean sensing | |
| Category | Sensing |
| Type | Boolean |
| Introduced in | 3.0 |
The online? block, referred to in its announcement post as the Network Connectivity Block,[1] is a Sensing block and a boolean block. The block returns whether or not the user is connected to the internet. It was added to prevent other methods of checking if the user is online (such as repeated use of the translate extension) from causing performance issues to the Scratch website.[2]
The block's return value is whether or not the device has an active internet connection (true or false); whether or not the internet connection is actively being used at the moment does not matter. A project could be loaded in a browser via the internet then later lose internet connection.
The problem with this block is that since it only reports if the device is connected to a network, it can’t detect whether Internet actually works or if it’s too weak to do anything.
Examples
- Check for connectivity before using cloud variables.
when gf clicked if <online?::sensing> then set [☁ var v] to (1) else say [You need to connect to a network to use cloud variables.] end
- Detect if offline mid-project.
when gf clicked forever if <<online?::sensing> = [false]> then . . . end end
Workarounds
- Main article: List of Block Workarounds#Sensing Blocks
| This block was designed to reduce server strain by replacing alternative methods of detecting if the user is online. It is best to use this block over its workarounds. |
This block can be somewhat replicated with the following code:
not <(translate [online?] to (Spanish v)) = ()> // The language can be any language you want. Translated text will be the empty string if the user is offline, as it must be translated on a remote server over the Internet.
However, repeated translations are cached, so to ensure the block continues working reliably when run multiple times:
not <(translate (days since 2000) to (Spanish v)) = ()> // Translate a different value every time to ensure a request is made over the Internet.