【问题标题】:Processing.js - Sleep, Wait, TimeOut, Pause, Delay?Processing.js - 睡眠、等待、超时、暂停、延迟?
【发布时间】:2011-09-01 18:04:59
【问题描述】:

Processing.js 有 sleep() 函数吗?如果不是,那么在 draw() 循环中添加延迟的合适替代方法是什么?

我正在使用带有处理功能的 JQuery - 我可以使用 JQuery 或 Javascript 函数来导致循环中的睡眠类型延迟吗?

谢谢!

【问题讨论】:

    标签: javascript jquery processing processing.js


    【解决方案1】:

    Processing 有一个 delay() 函数,但不幸的是,尚未在 Processing.js 中实现。

    您可以将 JS(JQuery 等)与 Processing 混合使用。 Processing 1.9.9 现在有 Javascript 模式,并且有处理/DOM 集成的示例,例如 SelectionFlower。 在sketch/pde file 中有一个方法设置被称为表单js:

    // called from JavaScript
    void setSelectionText ( String txt )
    {
        selectedText = txt;
    }
    

    并且在js文件中,设置了一个超时时间来确保sketch已经初始化并且可以被访问:

    var mySketchInstance;
    
    // called once the page has fully loaded
    window.onload = function () {
        getSketchInstance();
    }
    
    // this is called (repeatedly) to find the sketch
    function getSketchInstance() {
        var s = Processing.instances[0];
        if ( s == undefined ) {
            setTimeout(getSketchInstance, 200); // try again a bit later
    
        } else {
            mySketchInstance = s;
            monitorSelection();
        }
    }
    

    然后当草图实例可用时,您可以简单地调用草图上的方法/函数:

    function monitorSelection () {
    //bla bla
    mySketchInstance.setSelectionText(txt);  // set the text in the sketch
    }
    

    HTH

    【讨论】:

      【解决方案2】:

      这是我的解决方案。

      void waitasec (int sec) {
      
         int minutes = minute();
         int seconds = second();
         int hour = hour();
         int starttime = (hour * 3600) + (minutes * 60) + seconds;
         int finaltime = starttime + sec;
      
         while (starttime < finaltime) {
      
             minutes = minute();
             seconds = second();
             starttime = (hour * 3600) + (minutes * 60) + seconds;
         }
      }
      

      【讨论】:

        【解决方案3】:

        一个消耗资源的解决方案:

        int timer = 0;
        void draw() {
         if (timer%50 == 0) {
          //some code here
         }
         timer = timer +1;
        }
        

        【讨论】:

          【解决方案4】:

          jQuery

          .delay( 持续时间 [, queueName] )

          说明:设置一个定时器来延迟队列中后续项目的执行。

          查看链接http://api.jquery.com/delay/

          【讨论】:

            猜你喜欢
            • 2022-11-23
            • 2013-01-13
            • 1970-01-01
            • 1970-01-01
            • 2013-03-03
            • 1970-01-01
            • 1970-01-01
            • 2011-12-12
            • 1970-01-01
            相关资源
            最近更新 更多