为什么我的背景视频在警报之后播放



当前,我的背景视频只会在用户在警报弹出窗口中按"好"之后播放。如何创建代码以在窗口出现或页面加载后立即进行背景视频播放,而不必从警报中按OK。背景视频需要在警报出现之前进行播放。我是JavaScript的新手,因此我需要代码示例或对当前代码进行编辑才能正确理解。

<!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>Untitled Document</title>
</head>
<body>
<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();
        });
    });
}
window.onload = function() {
        document.getElementById("video");
    };
playAlert("Thank you.", 'Images/thankyou.wav?.wav?.wav?.wav?.wav')
.then(function() {
    return playAlert("Sorry, I haven't introduced myself.", 'Images/sorry.wav?.wav?.wav');
})
.then(function() {
    return myText("My name is Eve. What is your name?", 'Images/mynameiseve.wav?.wav?.wav');
})
.then(function() {
    return playAlert("Can you hear me?", 'Images/canyouhearme.wav?.wav');
});


</script>
<video controls id="video"  width="1300px" height="auto" preload="auto" autoplay="autoplay">
<source src="images/thirdeve.mp4" type="video/mp4"/>
</video>
</body>
</html>

如果您的JavaScript调用警报,则警报将是第一件事。在清除警报之前,什么都不会发生。

最新更新