所以我对javascript的知识非常有限,所以我提前为我的愚蠢道歉。
我正在努力使这些警报在网站最初打开后16秒播放。在后台,有一个视频播放,似乎视频对网站有影响,但实际上,它只是以特定的方式播放。我如何才能获得这三个警报从网站开放16秒后播放?你把settimeout放在哪里,或者你在代码中的哪个位置放置了函数的持续时间?
再次,我真的不知道javascript,所以请在哪里把setTimeout放在这段代码中?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>eve_</title>
<link rel="icon" rel="preload" href="images/evecircle.png" />
<style>
#video {
margin-left:-10px;
margin-top:-10px;
}
</style>
</head>
<script>
function myText() {
var txt;
var person = prompt("What's your name?", "");
if (person == null || person == "") {
txt = "User cancelled the prompt.";
} else {
txt = "Hello " + person + "! How are you today?";
}
document.getElementById("demo").innerHTML = txt;
}
function playAlert(msg, wav) {
return new Promise(function(resolve) {
var audio = new Audio(wav);
audio.addEventListener('canplay', function(e) {
audio.play();
alert(msg);
resolve();
});
});
}
playAlert("1", 'Images/thankyou.wav')
.then(function() {
return playAlert("2", 'Images/sorry.wav');
})
.then(function() {
return myText("3", 'Images/mynameiseve.wav');
});
</script>
<body>
<video autoplay="autoplay" preload="metadata" id="video" src="images/secondnew.mp4" width="1300px" height="auto" style="position:absolute; z-index:-1;" >
Video not supported.
</video>
</body>
</html>
答案 0 :(得分:0)
您可以在声明的函数之前使用它:playAlert()
所以:
let timeInMs = 16000; // set the time in Milisecond here
setTimeout(playAlert("1", 'Images/thankyou.wav'), timeInMs);
答案 1 :(得分:0)
使用&#34; ready&#34;文件的事件。 ---这个事件在DOM加载后触发。
function playAllAlerts(){
// do your things
}
document.addEventListener('DOMContentLoaded', function() {
setTimeout(playAllAlerts,16000)); // note playAllAlerts, not playAllAlerts()
});
function playAllAlerts(){
console.log('Thing 1');
console.log('Thing 2');
}
document.addEventListener('DOMContentLoaded', function() {
setTimeout(playAllAlerts,1000); // note playAllAlerts, not playAllAlerts()
});
&#13;
<div></div>
&#13;