Recently, I found a title console error when I was doing a project.
A look at the error file on the right is undefined
This is a distress, not the location of the problem.
Solve this problem today and share it.
The key to the problem is to execute the pause () method immediately after the play () method is executed. Vice versa
Paste the following code
Html:
<button id= "BTN1" >test</button>
Js:
var music=New Audio();
Music.Src="/1.mp3";This is replaced with a valid audio file address for easy testing
Music.loop=true var playstatus=false
function playorpause ({
Span class= "KW1" >if (Playstatus{
Music. Pause }else{
Music. Play
Playstatus=!playstatus;
/span>
for (Var i=0;i<10;i++) {document.queryselector ("#btn1"). Click ();}
Red Place code is continuous Click 10 #btn1 that is continuous rapid execution 10 times playorpause () will appear in Figure 1 Error situation (of course 2 will be error)
Of course, we can set a protective delay
As follows
var music=New Audio();
Music.Src="/1.mp3";
Music.Loop=True;
var playstatus=False;
var lastruntime=date.Now();
function Playorpause(){
var currenttime=date.Now();
var protecttime=100;Set protection delay unit milliseconds, not less than 50 recommendations 100 or more
If((currenttime-lastruntime)<p Rotecttime){
Return;Two executions are too frequent to exit directly
}
If(Playstatus){
Music.Pause();
}Else{
Music.Play
Playstatus=!playstatus;
Lastruntime=date. Now
for (var i =0;i<10 ;i++) {
Document. Queryselector "#btn1" Click
} /span>
But it's against the idea that we want to simulate a 10-click.
So you can use this method
var music=New Audio();
Music.Src="/1.mp3";
Music.Loop=True;
var playstatus=False;
var lastruntime=date.Now();
function Playorpause(){
var currenttime=date.Now();
var protecttime=100;Set protection delay unit milliseconds, not less than 50 recommendations 100 or more
If((currenttime-lastruntime)<protecttime){
Return;Two executions are too frequent to exit directly
}
If(Playstatus){
Music.Pause();
}Else{
Music.Play();
}
Playstatus=!playstatus;
LastRunTime=date.Now();
}
var counter=10;Number of points to be connected
var timer=setinterval(function () {
if (Counter>0{
document. queryselector ( "#btn1" Click }else{
Clearinterval
Counter--;
},500)
About Uncaught (in promise) Domexception:the play () request is interrupted by a call to pause () error