不知道将settimeout放在哪里 [英] No idea where to put settimeout

查看:106
本文介绍了不知道将settimeout放在哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对javascript的了解非常有限,因此我对自己的愚蠢表示歉意。

So I have very limited knowledge of javascript, so I apologize in advance for my stupidity.

我正在努力做到这一点,以便这些警报在16秒后播放该网站最初是打开的。在后台播放视频,似乎视频对网站有影响,但实际上,它只是以一种特定的方式播放。如何获取信息,以便三个警报在网站打开后的16秒内播放?

I am trying to make it so that these alerts play 16 seconds after the website is initially opened. In the background, there is a video playing and it seems like the video has an effect on the website, but in reality, it's just played in a particular way. How can I get it so that the three alerts play 16 seconds from the opening of the website? Where do you put the settimeout, or where in the code do you place the function's time duration?

再一次,我真的不知道javascript,所以请在哪里放置settimeout或在代码中的什么位置?

Once again, I truly do not know javascript, so please where do I put setTimeout in this code?

<!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>


推荐答案

使用文档的就绪事件。 ---在DOM加载后触发此事件。

Use the "ready" event of the document. --- This event fires after the DOM has loaded.

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()
    });

<div></div>

>

这篇关于不知道将settimeout放在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆