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

Does ThingWorx has full support of setTimeout() service?

SOLVED
Highlighted
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?


Tags (2)
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

Re: Does ThingWorx has full support of setTimeout() service?

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
Highlighted

Re: Does ThingWorx has full support of setTimeout() service?

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

Highlighted

Re: Does ThingWorx has full support of setTimeout() service?

There is!

It is working but it throws an exception.

The service is getting executed.

Highlighted

Re: Does ThingWorx has full support of setTimeout() service?

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.

Highlighted

Re: Does ThingWorx has full support of setTimeout() service?

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

Highlighted

Re: Does ThingWorx has full support of setTimeout() service?

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

Highlighted

Re: Does ThingWorx has full support of setTimeout() service?

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

Highlighted

Re: Does ThingWorx has full support of setTimeout() service?

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

Highlighted

Re: Does ThingWorx has full support of setTimeout() service?

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

Hope this helps in clarification.

Highlighted

Re: Does ThingWorx has full support of setTimeout() service?

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

Highlighted

Re: Does ThingWorx has full support of setTimeout() service?

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.

Announcements

Thingworx Navigate content has a new home! Click here to access the new Thingworx Navigate forum!