我正在寻找使处理程序OnClick只是随机实例,而不是需要用户交互。
// add the canvas in
document.body.appendChild(mainCanvas);
document.addEventListener('mouseup', createFirework, true);
document.addEventListener('touchend', createFirework, true);
// and now we set off
update();
}
/**
* Pass through function to create a
* new firework on touch / click
*/
function createFirework() {
createParticle();
}谢谢!:)
发布于 2012-12-28 05:34:20
只需从Jonathan那里扩展一个很好的解决方案:您不需要创建eventListeners递归randFunc()会在您退出浏览器之前随机调用。
我想你需要实现一种方法来停止这个函数。这里有一个解决方案:
var stopFW = false;
function randFunc () {
// stop execution
if (stopFW) return;
console.log("Called myself..." + new Date());
// And then instruct this function to be called between 0 and 10 seconds
setTimeout(randFunc, Math.ceil(Math.random() * 10) * 1000);
}
function stopFireWork() {
stopFW = !(stopFW);
console.log(stopFW);
}
<body onload="randFunc()">
<button onclick="stopFireWork()">Stop Firework</button>
</body>https://stackoverflow.com/questions/14060683
复制相似问题