我试图理解如何在AmplitudeJS中设置歌曲之间的时间延迟,但未成功。
Amplitude使用JSON文件定义歌曲和回调。就我而言,这是一个本地JSON文件:
从AmplitudeJS示例中:
var playCount = 0;
Amplitude.init({
"songs": [
{
"name": "Song Name 1",
"album": "Album Name",
"url": "/song/url.mp3",
"cover_art_url": "/cover/art/url.jpg"
},
{
"name": "Song Name 2",
"artist": "Artist Name",
"album": "Album Name",
"url": "/song/url.mp3",
"cover_art_url": "/cover/art/url.jpg"
},
{
"name": "Song Name 3",
"artist": "Artist Name",
"album": "Album Name",
"url": "/song/url.mp3",
"cover_art_url": "/cover/art/url.jpg"
}
],
//how the callback is setup in the example
"callbacks": {
'after_play': function(){
playCount++;
alert( playCount );
}
}
});
这是文档中有关振幅JS回调的报价:
Callbacks There are a variety of callbacks that AmplitudeJS calls at certain times and the developer can bind to.
| Callback | Description|
| --------------- |------------|
| before_play | Occurs before the play method is called|
| after_play | Occurs after the play method is called|
| before_stop | Occurs before the stop method is called|
| after_stop | Occurs after the stop method is called|
| time_update | Occurs when the time has updated|
| album_change | Occurs when an album changes|
| song_change | Occurs when a song has been changed|
To bind to a callback you add a function to your callbacks object with the key of one of the callbacks listed above. That key will be a function. When the callback is called, the function the user passes will be run.
我试图将我的setTimeout()方法添加到“song_change”回调中。根据在AmplitudeJS中的工作方式,您可以在JSON文件的末尾添加回调。像这样:
"callbacks": {
'after_play': function(){
playCount++;
alert( playCount );
}
}
我尝试过的许多事情之一是:
Var = timeOut(如Amplitude docs所示,它设置在JSON文件的顶部。
"callbacks": {
'song_change': function(){
timeOut= setTimeout (5000);
}
}
我只是想在歌曲之间设置一个延迟。
没事。是否有人对此问题有任何有用的评论或解决方案?我查看了堆栈溢出时的每个setTimeOut线程,并搜索了各种文档,但是我似乎无法理解如何将它们组合在一起。
我是一个内心的设计师。作为一名编码员,我不是很震惊。求求您了,请保持温柔,我不知道在其他地方可以找到好的帮助。我在尝试。关于AmplitudeJS的信息很少。我找不到专门的论坛。
TIA-雷切尔
最佳答案
我的处理方式是在歌曲更改后像这样进行回调:
"callbacks": {
'song_change': function(){
Amplitude.pause();
setTimeout (function(){
Amplitude.play();
}, 5000);
}
}
这是当歌曲改变,暂停 Activity 歌曲并设置超时时要做的。 5秒钟(5000毫秒)结束时,它将播放歌曲,并继续播放下一首歌曲。这应该提供适当的延迟。希望这可以帮助!
关于javascript - AmplitudeJS setTimeout()与JSON回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49785256/