cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can change your system assigned username to something more personal in your community settings. X

Does ThingWorx has full support of setTimeout() service?

aelgov
1-Newbie

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?


1 ACCEPTED SOLUTION

Accepted Solutions

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... ).

View solution in original post

10 REPLIES 10

There's no setTimeout service, neither you can use pause for it, you should build a queue system in order to do SetTimeout alike.

There is!

It is working but it throws an exception.

The service is getting executed.

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.

What you just explained make sense, but I saw code of TW using this setTimeout function.

If it's UI code ( extensions ) you may use at it's running at browser side, but not for the Server Side Javascript.

OK, Is there any place it is written - there is not support for setTimeout in TW server side JS?

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... ).

supandey
19-Tanzanite
(To:aelgov)

Hi Assaf, please visit this KCS document for best practice.

Hope this helps in clarification.

Tudor
12-Amethyst
(To:aelgov)

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);

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.

Top Tags