About Uncaught (in promise) Domexception:the play () request is interrupted by a call to pause () error

Source: Internet
Author: User

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)&lt;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.