【问题标题】:No idea where to put settimeout不知道在哪里放置 settimeout
【发布时间】:2018-09-09 22:48:06
【问题描述】:

所以我对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>

【问题讨论】:

    标签: javascript function time settimeout alert


    【解决方案1】:

    你可以在你声明的函数之前使用它:playAlert()

    所以:

    let timeInMs = 16000; // set the time in Milisecond here setTimeout(playAlert("1", 'Images/thankyou.wav'), timeInMs);

    【讨论】:

    • 喜欢这个?让 timeInMs = 3000; // 这里以毫秒为单位设置时间 setTimeout(playAlert("1", 'Images/thankyou.wav'), timeInMs); .then(function() { return playAlert("2", 'Images/sorry.wav'); }) .then(function() { return myText("3", 'Images/mynameiseve.wav'); }) ;
    【解决方案2】:

    使用文档的“就绪”事件。 --- 此事件在 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()
        });
    &lt;div&gt;&lt;/div&gt;

    【讨论】:

    • 是的,我想要他们三个,哈哈。但是让他们在 16 秒后开始
    • 您可以将其放在 HTML 末尾的脚本标签之间,或者放在文档末尾加载的单独文件中
    • 我修正了一个错字,然后我添加了一个代码 sn-p,它在一秒钟后记录了两件事。希望你能从中看到它是如何工作的。
    猜你喜欢
    • 1970-01-01
    • 2016-04-06
    • 2022-07-28
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    相关资源
    最近更新 更多