watch
[ Function ]
Version 1.10
public watch(
callback: (data: IWatchResultObject | IServerWatchResultObject) => void
): TcHmi.DestroyFunction;
Version 1.12
public watch<T = ST>(
callback: (data: IWatchResultObject<T> | IServerWatchResultObject<T>) => void
): TcHmi.DestroyFunction;
Monitors the symbol for changes.
The callback function is called either synchronously or asynchronously with the change, depending on the symbol type used.
Server | asynchron |
Internal | synchron |
LocalizedText | synchron |
PartialParam | synchron |
TemplateParam | synchron |
Function | synchron |
Control | synchron |
Version 1.12: If the API is used in TypeScript code, the symbol type (ST: SymbolType) can be reported to the TypeScript compiler when a symbol is created. This then no longer has to be specified when using this function, although it can be overwritten. |
This is a merely an auxiliary programming feature. The browser does not recognize this "type annotation". No check or conversion takes place at runtime. |
Parameter
Name | Type | Description |
---|---|---|
callback [ optional ] | Version 1.10 (data: IWatchResultObject | IServerWatchResultObject) => void Version 1.12 (data: IWatchResultObject<T> | IServerWatchResultObject<T>) => void | This is triggered when the symbol value changes or an error has occurred. The callback function is initially triggered once with the current value. For details, please refer to the parameter data. |
Return value
Type | Description |
---|---|
When this function is called, monitoring for this symbol is deactivated and all associated resources are released. If the callback function is called synchronously, the return value of the function cannot be accessed because it has not yet been defined. In this case, there is a reference to the function in the parameter of the callback function. |
Available from version 1.10 |
Sample - JavaScript
var symbol = new TcHmi.Symbol('%s%PLC1.MAIN.sTest%/s%');
var destroySymbol = symbol.watch(function (data) {
if (data.error === TcHmi.Errors.NONE) {
// Handle result value...
var value = data.value;
console.log(value);
} else {
// Handle error...
}
// Stop watch inline
// data.destroy(); // Call the destroy function inline to stop the watch and free resources.
});
// Stop watch
// destroySymbol(); // Call the destroy function to stop the watch and free resources.
Sample 1 - TypeScript
var symbol = new TcHmi.Symbol('%s%PLC1.MAIN.sTest%/s%');
var destroySymbol = symbol.watch(function (data) {
if (data.error === TcHmi.Errors.NONE) {
// Handle result value...
var value = data.value; // TS does not know the variable type
console.log(value);
} else {
// Handle error...
}
// Stop watch inline
// data.destroy(); // Call the destroy function inline to stop the watch and free resources.
});
// Stop watch
// destroySymbol(); // Call the destroy function to stop the watch and free resources.
Sample 2 - TypeScript
var symbol = new TcHmi.Symbol<string>('%s%PLC1.MAIN.sTest%/s%');
var destroySymbol = symbol.watch(function (data) {
if (data.error === TcHmi.Errors.NONE) {
// Handle result value...
let thisIsAString = data.value; // TS knows this is a string
console.log(thisIsAString);
} else {
// Handle error...
}
// Stop watch inline
// data.destroy(); // Call the destroy function inline to stop the watch and free resources.
});
// Stop watch
// destroySymbol(); // Call the destroy function to stop the watch and free resources.
Sample 3 - TypeScript
var symbol = new TcHmi.Symbol ('%s%PLC1.MAIN.sTest%/s%');
var destroySymbol = symbol.watch<string>(function (data) {
if (data.error === TcHmi.Errors.NONE) {
// Handle result value...
let thisIsAString = data.value; // TS knows this is a string
console.log(thisIsAString);
} else {
// Handle error...
}
// Stop watch inline
// data.destroy(); // Call the destroy function inline to stop the watch and free resources.
});
// Stop watch
// destroySymbol(); // Call the destroy function to stop the watch and free resources.