1

ユーザーの操作を必要とせずに、ハンドラー 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();
  }

ありがとう!:)

4

2 に答える 2

0

関数呼び出し自体はそれほど難しくありません。

// 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/

于 2012-12-27T20:24:44.930 に答える
0

Jonathan からの優れたソリューションを拡張するだけです。ブラウザを終了するまで、再帰的な randFunc() がランダムに呼び出される eventListeners を作成する必要はありません。

関数を停止する方法を実装する必要があると思います。ここに1つの解決策があります:

     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>
于 2012-12-27T21:34:20.273 に答える