Is there anything called "wait block" because I really need them

Is there something called “Wait blocks” I really need them
I tried but I have no idea how actually to do it
image

1 Like

Its been 4 hours and still no reply

Yeah there is something like what you’re trying to do… At least I believe that’s what you want.
you can try doing
setTimeout(function() { //code you want to execute }, 2000)
The 2000 in the setTimeout is a property that will set in how many seconds the function will be executed.
There is sadly still no function called wait, but if you want, I created one for you! (and other people who want to use it)
here’s the code:
function wait(time) {
var currentFPS = World.frameRate;
setTimeout(function() {
World.frameRate = currentFPS;
}, time);
World.frameRate = 0.0000000000000000001;
}`

So, to use it, you need to write it exactly like you wrote it. Just do:
wait(2000)
just that the time parameter is measured in milliseconds, that is used for you to set a time higher than 0. If you need to set it in seconds just multiply the number of seconds you want by 1000.

Also as you can’t set the World.frameRate to 0, I had to pick a very small parameter. Trust me, that won’t change a lot, as that World.frameRate is very small.

2 Likes

@torsle30,

Please let us know if you are using GameLab or AppLab (or something else) and it is also helpful if you send a link to your project. It looks like @IMPixel sent you some code that may help if you are in GameLab.

Thanks,

Mike

Javascript does not support wait functions. I came from roblox, so I know how miserable not having a wait/sleep function is.

In place of that, you can use setTimeout. There are some other solutions like busy waiting (which eats up resources). Or await/async, which does not exist in es5, and especially not code.org.

It is not uncommon for forum members to not answer questions because they don’t have a solution. (And in this case, they really don’t)

1 Like

I am using applab (Ignore the rest) and I need to type more to reply so here I need to type more to reply so here I need to type more to reply so here I need to type more to reply so here I need to type more to reply so here I need to type more to reply so here

Ok, thanks! (Ignore the rest) I need to type more to reply so here I need to type more to reply so here I need to type more to reply so here I need to type more to reply so here I need to type more to reply so here I need to type more to reply so here

I tried it, but it didn’t work, it feels like it waits waits 1e+999 years, it is atleast very long, is there a way to make it shorter? I need a like 1 or 5 second wait

Hmm… That’s quite weird… Could you please show me your project? Or your student’s?

Good suggestion by @IMPixel - it helps us troubleshoot if you can post the project here. Use the “Share” button at the top left of your project to get a link to the project.

This is a general function I have used in App Lab to “pause.” The function take as a parameter the number of milliseconds to wait (seconds * 1000).

function pause(howLong) {
timer = getTime();
while (((getTime() - timer) < howLong)){}
}

To pause for 2 seconds you would simply call:
pause(2000);

Not sure if this will help with that you are doing.

1 Like

This process is busy waiting, and is considered an antipattern. I don’t advise using it as not only does it make the browser essentially freeze, any other asynchronous tasks will fail as well.

As you can see here: https://studio.code.org/projects/applab/b1Jht_lJEi09g99GqdNTeMF57NalJqLeWQ5rEHp_PvY/view, 500 is logged after 1000 despite it being called before the “pause”.

There are alternatives such as app lab’s speed function but it really isn’t a replacement for wait/sleep

Thanks for posting this code. It helped me with my own App Lab model for students’ use.