# Global

> The APIs that reside in the global scope, which may be called without a namespace prefix.

- Source: https://titaniumsdk.com/docs/sdk/12.6.3/Global
- Titanium SDK version: 12.6.3
- Kind: module
- Platforms: Android, iPhone, iPad, macOS

Titanium provides a number of global built-in objects, detailed below.

#### String Utilities

Titanium includes several extra utility functions for formatting text, attached to the
global [String](https://titaniumsdk.com/docs/sdk/Global.String.md) object.

#### console

Titanium provides [console](https://titaniumsdk.com/docs/sdk/Global.Console.md) support familiar to many javascript developers
for logging at the toplevel, in addition to the [Titanium](https://titaniumsdk.com/docs/sdk/Titanium.API.md) logging facilities.

#### Timers

Titanium has built-in support for one-off and repeating timers:

* Use [setTimeout](https://titaniumsdk.com/docs/sdk/Global.md#setTimeout) to start a one-off timer.
* Use [setInterval](https://titaniumsdk.com/docs/sdk/Global.md#setInterval) to start a repeating timer.

#### Alert

Titanium has a built-in convenience function [alert](https://titaniumsdk.com/docs/sdk/Global.md#alert) which can be used as an alias
for the [AlertDialog](https://titaniumsdk.com/docs/sdk/Titanium.UI.AlertDialog.md) module.

#### Locale

The `L` macro can also be used as an alias for the [Titanium.Locale.getString](https://titaniumsdk.com/docs/sdk/Titanium.Locale.md#getString) method.

## Properties

### Buffer

- Type: buffer.Buffer
- Platforms: Android, iPhone, iPad, macOS

a global reference to the [Buffer](https://titaniumsdk.com/docs/sdk/buffer.Buffer.md) class.

### console

- Type: Global.Console
- Platforms: Android, iPhone, iPad, macOS

Console logging facilities.

### DIST_ADHOC

- Type: Boolean
- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK 9.0.0
- Permission: read-only

A special constant that is replaced during the app build with a boolean `true`/`false` value, based on whether build target is `'dist-adhoc'`.

The replacement is done by a Babel plugin during JS transpilation phase of the app build.
This is an easy way to guard deploy type-specific codepaths (and make it easier for Babel to strip away dead code paths).

This pre-define originally lived in Alloy compilation but has been migrated to the SDK build process.

### DIST_STORE

- Type: Boolean
- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK 9.0.0
- Permission: read-only

A special constant that is replaced during the app build with a boolean `true`/`false` value, based on whether build target is `'dist-appstore'` or `'dist-playstore'`.

The replacement is done by a Babel plugin during JS transpilation phase of the app build.
This is an easy way to guard deploy type-specific codepaths (and make it easier for Babel to strip away dead code paths).

This pre-define originally lived in Alloy compilation but has been migrated to the SDK build process.

### ENV_DEV

- Type: Boolean
- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK 9.0.0
- Permission: read-only

Alias for [Global.ENV_DEVELOPMENT](https://titaniumsdk.com/docs/sdk/Global.md#ENV_DEVELOPMENT)

### ENV_DEVELOPMENT

- Type: Boolean
- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK 9.0.0
- Permission: read-only

A special constant that is replaced during the app build with a boolean `true`/`false` value, based on whether deploy type is `'development'`. Typically true for simulator/emulator builds.

The replacement is done by a Babel plugin during JS transpilation phase of the app build.
This is an easy way to guard deploy type-specific codepaths (and make it easier for Babel to strip away dead code paths).

This pre-define originally lived in Alloy compilation but has been migrated to the SDK build process.

### ENV_PROD

- Type: Boolean
- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK 9.0.0
- Permission: read-only

Alias for [Global.ENV_PRODUCTION](https://titaniumsdk.com/docs/sdk/Global.md#ENV_PRODUCTION)

### ENV_PRODUCTION

- Type: Boolean
- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK 9.0.0
- Permission: read-only

A special constant that is replaced during the app build with a boolean `true`/`false` value, based on whether deploy type is `'production'`. Typically true for app store/ad-hoc builds.

The replacement is done by a Babel plugin during JS transpilation phase of the app build.
This is an easy way to guard deploy type-specific codepaths (and make it easier for Babel to strip away dead code paths).

This pre-define originally lived in Alloy compilation but has been migrated to the SDK build process.

### ENV_TEST

- Type: Boolean
- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK 9.0.0
- Permission: read-only

A special constant that is replaced during the app build with a boolean `true`/`false` value, based on whether deploy type is `'test'`. Typically true for device builds.

The replacement is done by a Babel plugin during JS transpilation phase of the app build.
This is an easy way to guard deploy type-specific codepaths (and make it easier for Babel to strip away dead code paths).

This pre-define originally lived in Alloy compilation but has been migrated to the SDK build process.

### global

- Type: Global
- Platforms: Android, iPhone, iPad, macOS

Reference to the global object itself.

### OS_ANDROID

- Type: Boolean
- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK 9.0.0
- Permission: read-only

A special constant that is replaced during the app build with a boolean `true`/`false` value.

This value is replaced by an injected boolean literal value indicating if the current platform is Android.

The replacement is done by a Babel plugin during JS transpilation phase of the app build.
This is an easy way to guard platform-specific codepaths (and make it easier for Babel to strip away dead code paths).

This pre-define originally lived in Alloy compilation but has been migrated to the SDK build process.

### OS_IOS

- Type: Boolean
- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK 9.0.0
- Permission: read-only

A special constant that is replaced during the app build with a boolean `true`/`false` value.

This value is replaced by an injected boolean literal value indicating if the current platform is ipad/iphone.

The replacement is done by a Babel plugin during JS transpilation phase of the app build.
This is an easy way to guard platform-specific codepaths (and make it easier for Babel to strip away dead code paths).

This pre-define originally lived in Alloy compilation but has been migrated to the SDK build process.

### OS_VERSION_MAJOR

- Type: Number
- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK 9.2.0
- Permission: read-only

The operation system's major version number.

This returns the same value as the [Titanium.Platform.versionMajor](https://titaniumsdk.com/docs/sdk/Titanium.Platform.md#versionMajor) property.

``` js
if (OS_IOS && (OS_VERSION_MAJOR >= 13)) {
    // Do something on iOS 13 or higher only.
}
```

### OS_VERSION_MINOR

- Type: Number
- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK 9.2.0
- Permission: read-only
- Default: `0`

The operating system's minor version number.

This returns the same value as the [Titanium.Platform.versionMinor](https://titaniumsdk.com/docs/sdk/Titanium.Platform.md#versionMinor) property.

Will return zero if the OS does not have a minor version.

### OS_VERSION_PATCH

- Type: Number
- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK 9.2.0
- Permission: read-only
- Default: `0`

The operating system's patch version number.

This returns the same value as the [Titanium.Platform.versionPatch](https://titaniumsdk.com/docs/sdk/Titanium.Platform.md#versionPatch) property.

Will return zero if the OS does not have a patch version.

### process

- Type: process
- Platforms: Android, iPhone, iPad, macOS

Reference to the global `process` object.

## Methods

### alert

- Platforms: Android, iPhone, iPad, macOS

Displays a pop-up alert dialog with the passed in `message`.

This function is a shortcut for creating and displaying an
[alert dialog](https://titaniumsdk.com/docs/sdk/Titanium.UI.AlertDialog.md). For example, the following two statements produce
the same result.

``` js
alert('Danger, Will Robinson!');

Ti.UI.createAlertDialog({ message: 'Danger, Will Robinson!' }).show();
```

Note that unlike a web browser-based version of `alert`, the method is asynchronous. However,
only one alert dialog will be visible and modal at a time.

Be aware that this method may be removed in the future.

Parameters:

- `message` (String): Alert message.

### clearInterval

- Platforms: Android, iPhone, iPad, macOS

Cancels an interval timer.

Parameters:

- `timerId` (Number, optional): Unique timer identifier returned by [setInterval](https://titaniumsdk.com/docs/sdk/Global.md#setInterval).

### clearTimeout

- Platforms: Android, iPhone, iPad, macOS

Cancels a one-time timer.

Parameters:

- `timerId` (Number, optional): Unique timer identifier returned by [setTimeout](https://titaniumsdk.com/docs/sdk/Global.md#setTimeout).

### decodeURIComponent

- Platforms: Android, iPhone, iPad, macOS

Replaces each escape sequence in the specified string, created using the `encodedURI`
method, with the character that it represents.

For more information, see the MDN website for
[encodeURIComponent](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent)
and [decodeURIComponent](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/decodeURIComponent).

Parameters:

- `encodedURI` (String): Text that optionally contains encoded escape sequences.

Returns: String - Decoded string.

### encodeURIComponent

- Platforms: Android, iPhone, iPad, macOS

Replaces each special character in the specified string with the equivalent URI escape
sequence. Useful for encoding URIs.

For more information, see the MDN website for
[encodeURIComponent](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent)
and [decodeURIComponent](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/decodeURIComponent).

Parameters:

- `string` (String): Text that optionally contains special characters.

Returns: String - Encoded string.

### L

- Platforms: Android, iPhone, iPad, macOS

An alias for [Titanium.Locale.getString](https://titaniumsdk.com/docs/sdk/Titanium.Locale.md#getString).

Parameters:

- `key` (String): Key used to lookup the localized string.
- `hint` (String, optional): Text to return if `key` is not found.

Returns: String - Localized string defined by `key`, or value of `hint` otherwise.

### require

- Platforms: Android, iPhone, iPad, macOS

Loads either a native Titanium module or a CommonJS module.

The `require` function takes a module identifier as its argument and returns an object with
references to the module's exported symbols.

#### ES6+ Syntax

Using Titanium SDK 7.1.0 and later, you can also use the ES6+ [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) and [`export`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export) syntax
by enabling transpiling via the `<transpile>true</transpile>` property in your tiapp.xml

#### Native Titanium Module

To load a native Titanium module:

  1. Install the module to the project or copy it to the Titanium SDK home directory.
  2. Add the module as a dependency to the project by modifying the `modules` section of the `tiapp.xml` file.
  3. Initialize the module with the `require` method by passing the module ID to the method.

For detailed instructions, refer to
[Using a Module](https://titaniumsdk.com/guide/Titanium_SDK/Titanium_SDK_How-tos/Using_Modules/Using_a_Module.html).

#### CommonJS Module

To load a local CommonJS module, place the CommonJS module in the `Resources` directory of
your project. Pass the `require` method the path to the file without the `Resources`
directory, platform-specific directory and the `.js` extension.

Note that the appropriate *platform-specific* `Resources` subdirectory is merged into the
main `Resources` directory at build time. For example, suppose the following files exist.

* `Resources/app.js`
* `Resources/ui/MainWindow.js`
* `Resources/iphone/ui/TaskWindow.js`
* `Resources/android/ui/TaskWindow.js`

The appropriate `TaskWindow.js` file is moved into the `Resources/ui` directory of the
built application. To include both the MainWindow module and the platform-specific
TaskWindow module from any JavaScript file in the project:

``` js
// load main window module from ui subdirectory
var mainWindow = require('ui/MainWindow');

// load platform-specific task window module
var taskWindow = require('ui/TaskWindow');
```

For more information, see the official [CommonJS](http://requirejs.org/docs/commonjs.html) website.

#### Alloy

For Alloy, place the CommonJS modules in the `app/lib` directory of your Alloy project,
then load the module with the `require` method without the `app/lib` path and `.js`
extension.

Starting with Alloy 1.5.0, you can add platform-specific directories to the `app/lib` directory.
Do not include the platform-specific directory when referencing the CommonJS module.

For example, a module file `app/lib/android/myModule/module.js` may be loaded by
`app/controllers/index.js` using:

``` js
require('myModule/module');
```

#### Android

Additionally, on Android, an absolute path to a module may be specified using a path
separator (`/`) at the beginning of the path.

For example, a module file `Resources/myModule/module.js` may be required by
`Resources/example/example.js` using either of the following:

``` js
require('../myModule/module')
require ('/myModule/module')
```

Parameters:

- `moduleId` (String): Native module ID or local path to a JavaScript file *minus* the `.js` extension.

Returns: any - Exported `exports` object of the required module.

Example: Require a Native Module

To load a native module with the ID `com.mycompany.module`:

``` js
var module = require('com.mycompany.module');
```

Example: Require a JavaScript Module

`myModule.js`:
``` js
exports.message = "hello world";
```

`app.js`:
``` js
var myModule = require('myModule');
alert(myModule.message);
```

### setInterval

- Platforms: Android, iPhone, iPad, macOS

Executes a function repeatedly with a fixed time delay between each call to that function.

Note that although the interval is not guaranteed to be exact, the interval between calls
will be no less than the specified delay.

Returns a unique timer identifier that can be passed to [clearInterval](https://titaniumsdk.com/docs/sdk/Global.md#clearInterval)
to cancel this timer.

For more information, see the MDN website for
[setInterval](https://developer.mozilla.org/en/DOM/window.setInterval).

Parameters:

- `function` (Callback): Function to call.
- `delay` (Number, optional): Time in milliseconds to wait between calls to function.

Returns: Number - Unique timer identifier.

Example: Update a label once every second, and stop at 10 seconds

``` js
var count = 0;
var timer = setInterval(function() {
    count++;
    label.text = 'count - ' + count;
    if (count == 10) {
        clearInterval(timer);
    }
}, 1000);
```

### setTimeout

- Platforms: Android, iPhone, iPad, macOS

Executes code or a function after a delay.

Note that although the timeout is not guaranteed to be exact, the delay before the
function is invoked will be no less than the specified delay.

Returns a unique timer identifier that can be passed to [clearTimeout](https://titaniumsdk.com/docs/sdk/Global.md#clearTimeout)
to cancel this timer.

For more information, see the MDN website for
[setTimeout](https://developer.mozilla.org/en/DOM/window.setTimeout).

Parameters:

- `function` (Callback): Code or function to call.
- `delay` (Number, optional): Time in milliseconds to wait before the function is called.

Returns: Number - Unique timer identifier.

Example: Execute a function in 500 milliseconds

``` js
setTimeout(function() {
    Ti.API.debug('Called using setTimeout');
}, 500);
```

---

- This corpus documents Titanium SDK 13.4.1. Anything here that does not name a version describes 13.4.1.
- Titanium is maintained by TiDev. The Appcelerator toolchain is retired: there is no `appc` CLI, no Appcelerator Studio and no hosted build service. The supported CLI is `titanium` on npm, invoked as `ti`.

