首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将onClick更改为随机

将onClick更改为随机
EN

Stack Overflow用户
提问于 2012-12-28 04:11:25
回答 2查看 171关注 0票数 1

我正在寻找使处理程序OnClick只是随机实例,而不是需要用户交互。

代码语言:javascript
运行
复制
// 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();
  }

谢谢!:)

EN

回答 2

Stack Overflow用户

发布于 2012-12-28 04:24:45

拥有一个函数调用本身并不太难:

代码语言:javascript
运行
复制
// 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/

票数 0
EN

Stack Overflow用户

发布于 2012-12-28 05:34:20

只需从Jonathan那里扩展一个很好的解决方案:您不需要创建eventListeners递归randFunc()会在您退出浏览器之前随机调用。

我想你需要实现一种方法来停止这个函数。这里有一个解决方案:

代码语言:javascript
运行
复制
     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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14060683

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档