In this instructional exercise, I will demonstrate to you how effortlessly you can play sound following couple of moments or some time defer effectively utilizing JavaScript.
Here we going to make a HTML document with a sound tag and a sound src record. At that point we will stack it in our program. From there on, We will make a JavaScript capacity to play that sound with some deferral so it plays after a specific timeframe.
Also Read:- How to Create Calculator Using HTML, CSS and JavaScript
To see the video instructional exercise play this video
Play sound following couple of moments in JavaScript
In this past post How to Play Audio After The Page Loads in HTML With JavaScript I have portrayed completely.
You can tap on the above connection to figure out how to play sound after page stack utilizing JavaScript.
<script type="text/javascript">
setTimeout(function(){
document.getElementById("id_here").play();
}, 8000)
</script>
You can see, here I have utilized setTimeout capacity to accomplish the postponement.
setTimeout(function(){
document.getElementById("id_here").play();
}, 8000);
Also Read:- How to Create Multiplication Table Using JavaScript
This is the primary capacity to play sound following 8 seconds.
setTimeout(function(){
/your code goes here
}, enter defer time in miliseconds)
You may likewise intrigued by,
Alarm Before Leaving A Web Page Using JavaScript jQuery
Base Sticky Music Player Source Code in JavaScript and CSS
To check if your capacity is working or not you can utilize comfort log.
console.log('your sound is begun a little while ago');
This line will include a reassure message when the sound begins playing.
Also Read:- How to Write Java Program to Print Armstrong numbers from 0 to 999
You can test the beneath code
<!DOCTYPE html>
<html>
<head>
<title>My Audio</title>
</head>
<body>
<audio src="mysong.mp3" id="my_audio" loop="loop"></audio>
<script type="text/javascript">
setTimeout(function(){
document.getElementById("my_audio").play();
console.log('your sound is begun a little while ago');
}, 8000)
</script>
</body>
</html>
Simply put your sound way in the src characteristic. circle will play the sound document over and over.