How execute a function only once?

edited March 2014 in How To...

I would like to know how to execute a function only once. If I put the function in setup(), it will be executed once - but only when the app is started. When you put the function in draw(), it's executed 30 times per second. How do I execute a function only once?



  • Answer ✓

    In the code below simple change callMethod to true and myMethod() will be executed once only.

    boolean callMethod = false;
    void setup(){
    void draw(){
        callMethod = false;
    void myMethod(){
      println("myMethod executed");
  • and if you only want draw() to execute once then use noLoop().

  • edited March 2014

    Like @koogs said: Place a noLoop() within setup():

    void setup(){
      noLoop();   // draw() will be called once only!
  • noLoop() will prevent draw() from doing its usual thing. If the user only targets his function, not the remainder of the draw() code, the method given by quark is the one to use.

    Moreover, one can reset callMethod to true upon some conditions or user input (key press, etc.).

  • Thanks. I do want draw() to be executed, so in this case the answer to remove the reference to the function once it's been called is the best solution.

Sign In or Register to comment.