How to make wait through script

Im using 1.19
I want there to be a delay in my script so that the clip plays an animation before moving to the next frame
I have tried Thread.sleep(millisiconds) since I heard that wick editor uses java but it gave me an error
image

1 Like

I use this:

stop()  
frmPauze = this
setTimeout(
    function(){
        frmPauze.play()
    }
, 1000 );

where, of course, the 1000 is the amount of milliseconds you want to wait…
Does this do what you want?

regards

Paul

do I use this like:

if (condition){
stop()
frmPauze = this
setTimeout(
function(){
frmPauze.play()
}
, 1000 );
gotoAndPlay(framenumber);
}

No, you would use it like:

if (condition){
    stop();
    setTimeout(
        () => {
            this.play();
        },
    3000)
}

I’ve not tested this, but is should work :slight_smile:

you may also make a counter based on update frame and fps

something like
myCounter+=1;
if (myCounter>=seconds*fps){
//do something
}

where seconds are the number of seconds you wanna wait, fps your current frames per seconds settings

of
if(myCounter>=n){
…
}

where n= seconds/fps
for a more precise controle

I found this function on StackOverFlow a while ago:

function wait(ms){
   var start = new Date().getTime();
   var end = start;
   while(end < start + ms) {
     end = new Date().getTime();
  }
}

Use it as shown:

wait(1000); //Waits 1 second
gotoAndStop(2);

Hope this helps!

probably not because it would freeze the entire wick editor site until the wait ends. if each script ran on a different thread then it’d work fine, but wick editor only runs on one thread so if a script gets in a loop forever then the entire website gets stuck forever.

also THERE NEEDS TO BE A BIG DISCLAIMER SOMEWHERE THAT SAYS "JAVA IS NOT THE SAME AS JAVASCRIPT. frankly i’m kinda tired of new people mistakenly thinking that java is just shorthand for javascript. i’m not angry at the beginners because it’s not their fault, but seriously i don’t want to have to tell every beginner the important fact that java != javascript. because then they might google “how to do this specific thing in java” when they mean javascript, and then get a solution which only works in java and try it, but then it doesn’t work.

also fun fact, in the early days of the internet, you could put java applets in the browsers. but then it got removed. so then after that you would need to use flash/javascript. but then now flash got removed. so now it’s just javascript.

anyway, i like the counter solution and the setTimeout solution. but i will try to fix tazer_900’s solution.

var delayedFuncs = [];

// This function would run the function "callback" after
// a specified time (ms) has passed
window.delay = function(ms, callback) {
  delayedFuncs.push([Date.now() + ms, callback]);
}

onEvent("update", function() {
  var now = Date.now();
  // this is going to remove items from the array as it is
  // iterating over it, so counting from end to start is a
  // neat trick to fix problems that would arise from this
  // situation
  for (let i = listeners.length - 1; i >= 0; i--) {
    let data = delayedFuncs[i];

    if (now >= data[0]) {
      data[1](); // run the function
      delayedFuncs.splice(i, 1); // remove item at index i
    }
  }
});

if this untested code actually works as intended, then you would be able to use it exactly like the setTimeout function

delay(1000, function() {
  alert("Hello, world!");
});

um… the benefits of using this over setTimeout is… uh… it stops it when you stop the project? and… uh… that’s basically it.

2 Likes

this sadly doesnt work, line 15 has an error ;(

i think i used the wrong variable, it’s supposed to be delayedFuncs instead of listeners.
it is untested

I use the timeline to make delays
e.g
My project has 60 fps.
on a separate wick object (will be named B for the sake of the explanation) B, will have 60 frames.
(Which equals to 1 second according to my project fps)

On the wick object A update script,
if (b.currentFrameNumber==60) {
// do something}

:)