Skip to main content
SSSSSSBenay
Known Participant
May 17, 2019
Answered

setTimeout not working in Animate HTML5 Canvas

  • May 17, 2019
  • 2 replies
  • 3116 views

Hello everyone,

I am trying to use the setTimeout event in order to delay the firing of a specific function on the click of a button. This is my code, and it is simply not working. The function "playgame" is firing instantly when the button is clicked, instead of waiting the delay. What is my error? Thanks in advance! (This code and all objects are inside frame1 of my Animate timeline.)

this.stop();

setTimeout(playgame,10000);

this.Startbutton.addEventListener("click", ColorsIn.bind(this));

function ColorsIn() {

....(various other tasks that I want to execute before "playgame" function...)

playgame();

}

function playgame(){

.

.

.

}

    This topic has been closed for replies.
    Correct answer JoãoCésar17023019

    Hi.

    That's nice you made it work using the mentioned framework.

    For future reference, your code could have been written like this:

    function ColorsIn()

    {

        setTimeout(playgame, 10000);

    }

    function playgame()

    {

        console.log("game started");

    }

    this.stop();

    this.Startbutton.addEventListener("click", ColorsIn.bind(this));

    Regards,

    JC

    2 replies

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    May 18, 2019

    Hi.

    That's nice you made it work using the mentioned framework.

    For future reference, your code could have been written like this:

    function ColorsIn()

    {

        setTimeout(playgame, 10000);

    }

    function playgame()

    {

        console.log("game started");

    }

    this.stop();

    this.Startbutton.addEventListener("click", ColorsIn.bind(this));

    Regards,

    JC

    SSSSSSBenay
    Known Participant
    May 18, 2019

    Oh thank you for this!

    Legend
    May 18, 2019

    You seem to have a very confused notion of what setTimeout actually does. I recommend reading the documentation for it.

    WindowOrWorkerGlobalScope.setTimeout() - Web APIs | MDN

    SSSSSSBenay
    Known Participant
    May 18, 2019

    Thank you for the resource!

    I was able to achieve what I wanted to using the ZIM framework and ZIM library (zimjs.com). The ZIM "timeout" (ZIM Documentation ) function was actually exactly what I was trying to achieve, in my confusion, with the "setTimeout" function.

    Legend
    May 18, 2019

    Please tell us that you didn't import the entire ZIM library for the sole purpose of accomplishing something so completely trivial.