【发布时间】:2011-09-01 18:04:59
【问题描述】:
Processing.js 有 sleep() 函数吗?如果不是,那么在 draw() 循环中添加延迟的合适替代方法是什么?
我正在使用带有处理功能的 JQuery - 我可以使用 JQuery 或 Javascript 函数来导致循环中的睡眠类型延迟吗?
谢谢!
【问题讨论】:
标签: javascript jquery processing processing.js
Processing.js 有 sleep() 函数吗?如果不是,那么在 draw() 循环中添加延迟的合适替代方法是什么?
我正在使用带有处理功能的 JQuery - 我可以使用 JQuery 或 Javascript 函数来导致循环中的睡眠类型延迟吗?
谢谢!
【问题讨论】:
标签: javascript jquery processing processing.js
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
【讨论】:
这是我的解决方案。
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;
}
}
【讨论】:
一个消耗资源的解决方案:
int timer = 0;
void draw() {
if (timer%50 == 0) {
//some code here
}
timer = timer +1;
}
【讨论】:
【讨论】: