Skip to content

Commit ca723d8

Browse files
authored
Update script.js
1 parent ec021c9 commit ca723d8

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed
Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
1-
// const audio = new Audio('caraudio.mp3');
2-
// var resp = audio.play();
3-
4-
// if(resp != undefined){
5-
// resp.then(_ => {
6-
// //autoplay starts
7-
// }).catch(error => {
8-
// //shoe error
9-
// });
10-
// }
11-
// audio.loop()=true;
12-
131
var audio = document.createElement("audio");
142
document.body.appendChild(audio);
15-
audio.src="caraudio.mp3"
3+
audio.src = "caraudio.mp3";
4+
5+
let isMoving = false;
6+
7+
// Start playing the audio when the mouse moves
8+
document.body.addEventListener("mousemove", function() {
9+
if (!isMoving) {
10+
audio.play();
11+
audio.loop = true; // Set the audio to loop
12+
isMoving = true; // Indicate that the car is moving
13+
}
14+
});
1615

16+
// Stop playing the audio when the left mouse button is released
17+
document.body.addEventListener("mouseup", function(event) {
18+
if (event.button === 0) { // 0 is the left mouse button
19+
audio.pause();
20+
audio.currentTime = 0; // Optionally reset audio to the beginning
21+
isMoving = false; // Indicate that the car has stopped
22+
}
23+
});
1724

18-
document.body.addEventListener("mousemove", function(){
19-
audio.play();
20-
audio.loop();
21-
})
25+
// Optional: Start playing audio again if the mouse is moved while stopped
26+
document.body.addEventListener("mousedown", function() {
27+
if (!isMoving) {
28+
audio.play();
29+
audio.loop = true;
30+
isMoving = true;
31+
}
32+
});

0 commit comments

Comments
 (0)