如何将回调应用于多个元素?

时间:2015-06-04 09:01:32

标签: javascript arrays json callback

我有以下回调。但是,如果我开始我的播放器将向我展示第二次回调。如何进行回调并随机显示两个视频?

callback('{"video": {"mp4": "http://c0026122.cdn1.cloudfiles.rackspacecloud.com/193807.mp4", "webm": "http://5860e9e4db2f4cebe1e6-cc12bb9b5e092d34d0fadb7ce5f280a3.r47.cf1.rackcdn.com/193807.webm", "title": "Touch"}}');
callback('{"video": {"mp4": "http://c0026125.cdn1.cloudfiles.rackspacecloud.com/064915.mp4", "webm": "http://a95a046574a64b9ea43b-53264ce826f5cfe73ee69a0c3c37eccc.r68.cf1.rackcdn.com/64915.webm", "title": "Grace / Fever / Dinner / Heart to Heart / Videogame"}}');
return;

1 个答案:

答案 0 :(得分:2)

如果你问我认为你在想什么,并且想要显示一个随机视频,那么你可能需要考虑从一系列视频中生成一个随机数。

var videos = [ // Place any and all videos in this array
    {"video": {"mp4": "http://c0026122.cdn1.cloudfiles.rackspacecloud.com/193807.mp4", "webm": "http://5860e9e4db2f4cebe1e6-cc12bb9b5e092d34d0fadb7ce5f280a3.r47.cf1.rackcdn.com/193807.webm", "title": "Touch"}},
    {"video": {"mp4": "http://c0026125.cdn1.cloudfiles.rackspacecloud.com/064915.mp4", "webm": "http://a95a046574a64b9ea43b-53264ce826f5cfe73ee69a0c3c37eccc.r68.cf1.rackcdn.com/64915.webm", "title": "Grace / Fever / Dinner / Heart to Heart / Videogame"}}
];

var rand = Math.floor(Math.random() * videos.length); // Generate random number based on number of videos

callback(videos[rand]); // Run the callback with a random video