The APIs that reside in the global scope, which may be called without a namespace prefix.
Titanium provides a number of global built-in objects, detailed below.
Titanium provides a built-in JSON object two functions, parse
and stringify
.
Titanium includes several extra utility functions for formatting text, attached to the global String object.
Titanium provides console support familiar to many javascript developers for logging at the toplevel, in addition to the Titanium.API logging facilities.
Titanium has built-in support for one-off and repeating timers:
Use setTimeout to start a one-off timer.
Use setInterval to start a repeating timer.
Titanium has a built-in convenience function alert which can be used as an alias for the AlertDialog module.
The L
macro can also be used as an alias for the Titanium.Locale.getString method.
An alias for Titanium.Locale.getString.
Key used to lookup the localized string.
Text to return if key
is not found.
Localized string defined by key
, or value of hint
otherwise.
Displays a pop-up alert dialog with the passed in message
.
This function is a shortcut for creating and displaying an alert dialog. For example, the following two statements produce the same result.
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.
Alert message.
Cancels an interval timer.
Unique timer identifier returned by setInterval.
Cancels a one-time timer.
Unique timer identifier returned by setTimeout.
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 and decodeURIComponent.
Text that optionally contains encoded escape sequences.
Decoded string.
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 and decodeURIComponent.
Text that optionally contains special characters.
Encoded string.
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.
To load a native Titanium module:
modules
section of the tiapp.xml
file.require
method by passing the module ID to the method.For detailed instructions, refer to Using a 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:
// 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 website about CommonJS Modules/1.1 Specification.
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:
require('myModule/module');
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:
require('../myModule/module')
require ('/myModule/module')
To load a native module with the ID com.mycompany.module
:
var module = require('com.mycompany.module');
myModule.js
:
exports.message = "hello world";
app.js
:
var myModule = require('myModule');
alert(myModule.message);
Native module ID or local path to a JavaScript file minus the .js
extension.
Exported exports
object of the required module.
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 to cancel this timer.
For more information, see the MDN website for setInterval.
var count = 0;
var timer = setInterval(function(){
count++;
label.text = "count: " + count;
if (count == 10) {
clearInterval(timer);
}
}, 1000);
Function to call.
Time in milliseconds to wait between calls to function.
Unique timer identifier.
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 to cancel this timer.
For more information, see the MDN website for setTimeout.
setTimeout(function(){
Ti.API.debug('Called using setTimeout');
}, 500);
Code or function to call.
Time in milliseconds to wait before the function is called.
Unique timer identifier.