将onClick更改为随机
Changing onClick to be random
我希望使处理程序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();
}
谢谢!:)
拥有一个函数调用本身并不太难:
// This function executes itself the first time immediately
(function randFunc () {
// We output some information to the console
console.log("Called myself..." + new Date());
// And then instruct this function to be called between 0 and 10 seconds
setTimeout(randFunc, Math.random() * 10 * 1000);
}());
演示:http://jsfiddle.net/8tZEu/2/
只是从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>
相关文章:
- 如何在执行此特定onclick事件时执行JavaScript函数
- Setting default onclick behavior for <img> tag in gene
- Javascript全局onclick监听器
- 如何调用“;链接_;在onclick事件上使用Javascript
- onclick函数需要双击,因为类分配延迟
- 如何使用jquery迭代具有相同属性的html元素并查找onclick事件
- 使用onclick绘制SVG路径
- $(document).height()在刷新时随机化值(Safari 5.1.10)
- 使用当前日期生成随机id
- 如何将数据从onclick按钮传递到变量
- 选择onclick事件jquery
- JavaScript OnClick按钮生成随机算术问题表
- jQuery onclick加载随机使用火狐浏览器
- 分配变量OnClick随机动作
- 从图像切换到随机图像onclick与JavaScript
- Javascript随机引用onclick
- Javascript onclick随机工作
- 如何将随机、连续的视频自动播放添加到现有的 onClick 视频事件中
- 在博客上随机加载帖子(将onclick更改为onload)
- 将onClick更改为随机