Does ThingWorx has full support of setTimeout() service?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Does ThingWorx has full support of setTimeout() service?
Hi,
Everytime I use setTimeout() function in all the option it can be used it throws an exeption: ReferenceError: "setTimeout" is not defined.
I noticed also that the function inside the setTimeout DOES run, so I need to use try and catch in all the places.
I wonder whether I use the wrong syntax for setTimeout, I think I tried them all
setTimeout(nameOfFunction(),1000);
setTimeout("nameOfFunction()",1000);
setTimeout(nameOfFunction,1000);
setTimeout("nameOfFunction",1000);
setTimeout(function {nameOfFunction()},1000);
setTimeout(new function {nameOfFunction()},1000);
and some other options.
I really don't want to use pause.
It there any suggestion?
Solved! Go to Solution.
- Labels:
-
Design
- Tags:
- javascript
- services
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
I don't think so, neither the other way ( saying it has it ). Server Side Javascript runs on a RHINO javascript engine, you can do a search on that mater ( I did it and it says there isn't setTimeout - it shows up some alternative ways that you may give it a shot... ).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
There's no setTimeout service, neither you can use pause for it, you should build a queue system in order to do SetTimeout alike.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
There is!
It is working but it throws an exception.
The service is getting executed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hi Assaf, no there isn't.
It throws and error becouse it doesn't exists, but of course if you execute something like setTimeout(aFunction(),1000) it will execute aFunction() before trying to execute setTimeout, for instance you can think that this is working as setTimeout but it isn't:
function TesTimedService() {
logger.info("LOG MESSAGE");
}
try{
setTimeout(TesTimedService(),1000);
}catch(err) {
}
Standard Javascript setTimeout works receiving a function as a parameter, which doesn't execute anything in TW server side javascript:
try{
setTimeout(function TesTimedService() { logger.info("LOG MESSAGE 2");},1000);
}catch(err) {
}
From previous code which would be correct setTimeout call you don't get "LOG MESSAGE 2" on the logs as it's never executed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
What you just explained make sense, but I saw code of TW using this setTimeout function.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
If it's UI code ( extensions ) you may use at it's running at browser side, but not for the Server Side Javascript.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
OK, Is there any place it is written - there is not support for setTimeout in TW server side JS?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
I don't think so, neither the other way ( saying it has it ). Server Side Javascript runs on a RHINO javascript engine, you can do a search on that mater ( I did it and it says there isn't setTimeout - it shows up some alternative ways that you may give it a shot... ).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hi Assaf, please visit this KCS document for best practice.
Hope this helps in clarification.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
As Charles noted above, there is no timeout support in Rhino. One potential workaround we implemented in other Rhino environments to address this scenario is to create a loop that iterates for the duration of your timeout:
var date = new Date();
var curDate = null;
do { curDate = new Date(); }
while(curDate-date < 60000);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hi Tudor,
This code you wrote here consumes 100% of CPU, you should never write this kind of code.
It's not recomended but you have a pause() service on RHINO which won't use any CPU resources.
Carles.