javascript Processing.js - 睡眠、等待、超时、暂停、延迟?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7274509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:33:35  来源:igfitidea点击:

Processing.js - Sleep, Wait, TimeOut, Pause, Delay?

javascriptjqueryprocessingprocessing.js

提问by logic-unit

Is there a sleep() function for Processing.js? If not what would be a suitable alternative to add a delay in the draw() loop?

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

I am using JQuery with Processing - can I use a JQuery or Javascript function to cause a sleep type delay in the loop?

我正在使用 JQuery 和 Processing - 我可以使用 JQuery 或 Javascript 函数在循环中导致睡眠类型延迟吗?

Thanks!

谢谢!

采纳答案by George Profenza

Processing has a delay()function but unfortunately that is not implemented into Processing.js yet.

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

You can mix JS(JQuery,etc.) with Processing though. Processing 1.9.9has a Javascript mode now and there are examples for Processing/DOM integration, like SelectionFlower. In the sketch/pde filethere is a method setup to be called form js:

不过,您可以将 JS(JQuery 等)与 Processing 混合使用。 Processing 1.9.9现在有一个 Javascript 模式,并且有 Processing/DOM 集成的例子,比如SelectionFlower。在Sketch/pde 文件中,有一个名为 form js 的方法设置:

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

and in the js file, a timeout is set to make sure the sketch is initialized and can be accessed:

在 js 文件中,设置超时以确保草图已初始化并可访问:

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();
    }
}

Then when the sketch instance is available, you can simply call a method/function on the sketch:

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

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

HTH

HTH

回答by m24thompson

Here is my solution.

这是我的解决方案。

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;
   }
}

回答by Giorgio

A resource consuming solution:

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

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

回答by PinkSheep

jQuery

jQuery

.delay( duration [, queueName] )

.延迟(持续时间[,队列名称])

Description:Set a timer to delay execution of subsequent items in the queue.

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

See the link http://api.jquery.com/delay/

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