0

私はphpを使用して、次のようなサウンドを作成および再生するJavaScriptを生成しています。

$playsound .= "var $sound = new Audio('http://$siteurl/sounds/$sound.ogg'); soundplayer($sound $whentostart, $whentostop);   ";

soundplayerは、サウンド名と、開始および停止するタイミングの期間をパラメーターとして受け取るjavascript関数です。

私が抱えている問題は、一緒に演奏したいサウンドプレーヤーのグループ(パラメーター付き)があることです...つまり、特定のグループが同時にサウンドを演奏します。その後、ある間隔で開始する必要がある他のグループ。

例:このグループの再生:soundplayer(with params)soundplayer(with params)

たとえば1〜4秒の間の時間

次に、このグループが再生されます:soundplayer(with params)soundplayer(with params)

その後、同じ数のグループに対して繰り返します

jqueryまたはjavascriptで、soundplayer()グループへの各関数呼び出しをラップして、各グループを特定の間隔で再生させるにはどうすればよいですか?

4

1 に答える 1

1

あなたはsetTimeout(連鎖)またはsetInterval(あなたがそれを止めるまで繰り返す)のどちらかでそれをすることができます。

JavaScriptで、そして概念的には、物事を単純に保つために:

// Put the things you want to do in a function
function doThingsInOrder() {
    doOneThing('someArgument');
    doAnotherThing();
    doAThirdThing('someArgument');
    // Maybe one of them should be delayed a bit
    setTimeout(doADelayedThing, 200); // 200 = 200ms = 1/5th second
}

// Call the function every four seconds, forever
setInterval(doThingsInOrder, 4000);

それがsetIntervalバージョンです。このsetTimeoutバージョンでは、最後にdoThingsInOrder経由で自分自身を呼び出すだけです。setTimeout

// Put the things you want to do in a function
function doThingsInOrder() {
    doOneThing('someArgument');
    doAnotherThing();
    doAThirdThing('someArgument');
    // Maybe one of them should be delayed a bit
    setTimeout(doADelayedThing, 200); // 200 = 200ms = 1/5th second

    // Set up next call
    setTimeout(doThingsInOrder, 4000);
}

// Start the process off
setTimeout(doThingsInOrder, 4000);
于 2012-05-15T04:55:44.477 に答える