我有一个函数,可以以非重复的方式显示数组中的随机单词。
我还想为每个单词播放一个声音剪辑(该声音将作为单词的发音)。
我知道如何播放单个声音,以及如何从一系列声音中播放随机声音。但是,如果我创建声音数组,那么只有在显示相应的单词时才可以播放每个声音吗?
这就是我正在使用的:
const p = document.getElementById("randomWord");
const origWords = ["alpha", "bravo", "charlie", "delta", "echo"];
const audioClips = ["alpha.mp3", "bravo.mp3", "charlie.mp3", "delta.mp3", "echo.mp3"];
let remainingWords = [];
function randomize() {
if (remainingWords.length === 0) remainingWords = origWords.slice();
const {
length
} = remainingWords;
const [word] = remainingWords.splice(Math.floor(Math.random() * length), 1);
p.textContent = word;
}
<button onclick="randomize()" type="button">Random Word</button>
<p id="randomWord"></p>
答案 0 :(得分:2)
我会更改您存储数据的方式。除了使用int value = MEASURE(init, 1, 1.2);
和template<class T>
struct MeasureTool {
template <class F, class... Args>
auto measure(F &&fn, Args... args) {
tools::TimeMeasure timeMeasure;
return (static_cast<T*>(this)->*fn)(std::forward<Args>(args)...);
}
};
class Test : public MeasureTool<Test>
{
public:
Test();
virtual ~Test() {}
int init(const std::string &filepath) { _path = filepath; return 0; }
const auto &getPath() const { return _path; }
private:
std::string _path;
};
Test::Test()
{
std::string filepath("/some/where");
int value = measure(&Test::init, filepath);
measure(&Test::getPath);
}
之外,您还可以使用对象数组:
@ManyToOne
然后在获得随机索引后,在函数中只需按@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(referencedColumnName = "id",name = "id_profesor", insertable=false, updatable=false)
private Profesor profesor;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(referencedColumnName = "id",name = "id_asignatura", insertable=false, updatable=false)
private Asignatura asignatura;
或origWords
键
编辑:摘要
audioClips
const myWords = [
{
text: "alpha",
audio: "alpha.mp3"
},
{
text: "bravo",
audio: "bravo.mp3"
},
...
]
答案 1 :(得分:0)
我将使用一个哈希图,例如:
const origWords = {"alpha":"alpha.mp3", "bravo":"bravo.mp3", "charlie":"charlie.mp3", "delta":"delta.mp3", "echo":"echo.mp3"};
...
randomIndex = randomize();
key = Object.keys(origWords)[randomIndex];
p.textContent = key;
playSound(origWords[key]);
...