Whats best way to add delay in javascript


#1

Whats the best way to add a short delay in javascript ? Something like llSleep.
Afraid without delay the while loop is to fast.


#2

See JavaScript’s setTimeout method.


#3

@ctrlaltdavid , getting error.

line 65 :  “ReferenceError: Can’t find variable: setTimeout”

this the line:

setTimeout(doorAction(i), 250);

setTimeout not supported in High Fidelity ?
w3school is saying, “The window.setTimeout() method can be written without the window prefix.”


#4

What you want is Script.setTimeout().

You may also find currentAPI.js useful: it lists all, or at least most of, the current script objects and methods - incl. setTimeout().


#5

Just found the problem, did a search in alle xample scripts. looks like we need

Script.setTimeout(Action(i), 250);

Then i dont get the error anymore. tried it now with.

Script.setTimeout(function(){}, 500);

Looks i need to dig in the api.


#6

@ctrlaltdavid , no currentAPI.js is not really usefull its only 1.1 KB big and not much inside. checkled the git one i have and the one i have downloaded out of interface.exe. but or very empty. you mean this api !

https://github.com/highfidelity/hifi/wiki/Javascript-API


#7

@richardus what @ctrlaltdavid means, if you run currentapi.js (e.g. in the script editor) it prints out a list of functions.


#8

[Combined seperate messages to one]

@chris, aha. but told you before i cannot use the internal script editor. doing everything with notepad++. just because the color , font and size of the internal editor are not workable for me.
some colors have barly contrast with the background in the internal editor.

Anyway, good to know !n I can run it and save that to disk i think.

Ok, i have started currentapi.js. and have the list. for others.
You need to retrieve the list out of the log file in the viewer. its not saveing it to a file, but only displaying it on the console.

Found a good example:

timer.js

Now i need to figure out why it dont work in my script but
works in timer.js after disabling the timer stop commands.
Ill figure it out, now have a reference file.

Want to thanks everybody for the great help and patience


#10

Can someone try this script that use setInterval and learInterval…
I dont see anything wrong, but the whole interface.exe is crashing.


#11

Just ran the script - am getting same result, Interface crashes. I can’t see anything wrong with the code - almost certainly a bug…

  • davedub

#12

Thanks , davedub. now its waiting until the have fixt it before i can proceed with scripting.
Going for plan B, while figureing everything oput i know what did go wrong at the first time before the timer idea. so doing it without timer now.


#13

Re timer.js script:

The bits of code are ok but the script is self cancelling the moment it starts,
Comment out the 2 clear functions and the timers work fine.

var one_timer = Script.setTimeout(function() { print("One time timer fired!"); }, 10000);
var multiple_timer = Script.setInterval(function() { print("Repeating timer fired!"); }, 1000);

// this would stop a scheduled single shot timer
//Script.clearTimeout(one_timer);

// this stops the repeating timer
//Script.clearInterval(multiple_timer);

The Script.clearTimeout command only needs to be used if you want the option to turn off the timer before it fires, left to normal play the Script.setTimeout will fire once (after its pause) then thats all, you might build it into a loop so it fires every iteration. But it does not need to be cleared.

The Script.setInterval will fire continuously at regular intervals until clearInterval is called,
The clearInterval needs to be moved into its own function and called after some period of running.


#14

Could not access the worklist anymore, i used this as basic example. normally the clearInterval is called much later after actions are done in my oter script. but on both cases it did crash interface.exe. so if this example below cannot run, it never can work. it crash as soon clearInterval is called.

var timer;

function test()
{
    Script.clearInterval(timer)
    print(‘End’);
    Script.stop();
}

timer=Script.setInterval(function() { test() }, 4000);


#15

Yes I have to agree after some testing the Script.clearInterval seems to crash every time.


#16

My bad guys.
Only fixed part of the issue yesterday and since the other one was a concurrency issue that didn’t happen to me after my fix, I missed it.

Got a PR up for it here
Since we have demos today and tomorrow, it’ll be merge over the week end.