To minimize the communication frequency between the logic layer and the view layer during frequent interactions on a page, use the hjs function to handle page events directly. The hjs code can be written within the hjs tag of an .hxml file or in a standalone .hjs file.
getComputedStyle(Array<string> attrs): Object
Obtains the style of an element based on the specified style name.
Since: 1.0.8
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| attrs | Array<string> | Yes | Element style name, for example, ['width', 'height', 'backgroundColor']. |
Returns:
An object, for example, { 'width':'100px', 'height': '100px', 'backgroundColor':'color value' }.
Example:
var computedStyle = function(event, ownerInstance) {
if (ownerInstance.getComputedStyle) {
// Check whether the value is empty.
var result = ownerInstance.getComputedStyle(['width', 'height', 'backgroundColor']);
console.info('getComputedStyle result:', result);
}
};
module.exports = {
getComputedStyle: computedStyle
}; setTimeout(function callback, number delay, any rest): number
Creates a timer.
Since: 1.0.8
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Callback used to return the result. |
| delay | number | No | Delay, in milliseconds, before the function is called. It is a string that supports numeric values. If it is left empty or if a non-numeric value is passed, it defaults to 0. |
| rest | any | No | Additional parameters such as param1 and param2, which are passed to the callback as arguments. |
Returns:
A timer ID of the number type, which can be passed to clearTimeout to cancel the timer.
Example:
var timeout = function(event, ownerInstance) {
if (ownerInstance.setTimeout) {
// Check whether the value is empty.
var id = ownerInstance.setTimeout(function() {
console.info('The timer is executed');
}, 1000);
console.info(`The timer id is: ${id}`);
}
};
module.exports = {
setTimeout: timeout
}; clearTimeout(number id): void
Clears the timer.
Since: 1.0.8
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| id | number | Yes | ID of the timer to be cleared. |
Example:
var clearTimeout = function(event, ownerInstance) {
if (ownerInstance.clearTimeout) {
// Check whether the value is empty.
var id = ownerInstance.setTimeout(function() {
console.info('The timer is executed');
}, 1000);
console.info(`The timer id is: ${id}`);
ownerInstance.clearTimeout(id);
}
};
module.exports = {
clearTimeout: clearTimeout
}; getBoundingClientRect(): Object
Obtains the position and size information of a page or component.
Since: 1.0.8
Returns:
An object containing the following system information fields.
| Attribute | Type | Description |
|---|---|---|
| x | number | Horizontal coordinate of the element's upper left corner relative to the view. |
| y | number | Vertical coordinate of the element's upper left corner relative to the view. |
| width | number | Width of an element, in pixels. |
| height | number | Height of an element, in pixels. |
| left | number | Distance from the left edge of the element to the left edge of the view, in pixels. |
| top | number | Distance from the top of the element to the top of the view, in pixels. |
| right | number | Distance from the right edge of the element to the left edge of the view, in pixels. |
| bottom | number | Distance from the bottom of the element to the top of the view, in pixels. |
Example:
var boundingClientRect = function(event, ownerInstance) {
if (ownerInstance.getBoundingClientRect) {
// Check whether the value is empty.
var result = ownerInstance.getBoundingClientRect();
console.info('getBoundingClientRect result:', result);
}
};
module.exports = {
getBoundingClientRect: boundingClientRect
};