Using the power of Ionic 3 and Cordova, you can implement a type script button that

I'm facing an issue with multiple buttons on a page, where each button plays audio when clicked or long pressed. The problem is that when one audio is playing and another button is clicked, the new audio overlaps with the previous one. How can I prevent this from happening?

Here is the HTML code:

<ion-grid style = "width: 80%;"  >
<div>
<ion-row  >

      <ion-col (click)="p10_1()" (press)= "p10_1l()" >

      <div id  = "container" >
      <div class = "sections" id = "w1pl1" >
       ـرَ  
      </div><!--
     --><div class = "sections" id = "w1l2" >
         مَـ  
      </div><!--
      --><div class = "sections" id = "w1l3" >
           اَ  
      </div>

</div>    

 <div id  = "container" (click)="p10_2()" (press)= "p10_2l()">
            <div class = "sections" id = "w2l1" >
              نَ  
      </div>
      <div class = "sections" id = "w2l2" >
           ذِ  
      </div>
      <div class = "sections" id = "w2l3" >
           اَ  
      </div>

    </div>    

Here is the TypeScript file (.ts):

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { NativeAudio } from '@ionic-native/native-audio';


@Component({
  selector: 'page-home',
  templateUrl: 'home.html'

})
 export class HomePage {

  constructor(public navCtrl: NavController) {

  }


  p10_1()
  {

      var bleep = new Audio();
      bleep.src = './assets/sounds/q1p10_1.mp3';
      bleep.play();

      }

   p10_1l()
  {
      var bleep = new Audio();
      bleep.src = './assets/sounds/q1p10_1l.mp3';
      bleep.play();

      }





  p10_2()
  {
      var bleep = new Audio();
      bleep.src = './assets/sounds/q1p10_2.mp3';
      bleep.play();
      }

   p10_2l()
  {
      var bleep = new Audio();
      bleep.src = './assets/sounds/q1p10_2l.mp3';
      bleep.play();
      }

Thank you in advance.

Answer №1

Make sure to include bleep.stop(); before setting the source of bleep.

p10_1()
  {
      var bleep = new Audio();
      bleep.stop();
      bleep.src = './assets/sounds/q1p10_1.mp3';
      bleep.play();
      }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display or hide an icon based on the location's search parameters

Taking the scenario where I have a page and plan to access it in two different ways: Accessing it as is. Appending additional information to the URL, for example: , manually inputting the value "?nohome" in the code. Next step would involve implementin ...

Acquiring variables from a JQuery Ajax request while invoking a JavaScript file

I'm currently experimenting with using JQuery Ajax to retrieve a javascript file. I need to pass some parameters to it, but I'm not entirely sure how to do so in Javascript (PHP seems easier for this). Here is my current setup: function getDocum ...

Issue with setTimeout function when used in conjunction with an ajax call

Currently, I am developing a quiz portal where questions are organized in modules. Each module consists of 5 questions: the first 4 are text-based, and the 5th is an image-based question. Upon registering through register.php, users are directed to index. ...

Browser refusing to acknowledge jQuery/JavaScript (bootstrap)

Here is where I include the necessary jQuery libraries and my custom jQuery code: <script src="//code.jquery.com/jquery-1.11.0.min.js"></script> <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script> <script src= ...

Can the numbers and mathematical operators be sequentially extracted from an array?

Currently, I am in the process of creating a calculator using HTML, CSS, and JavaScript (specifically jQuery). My main focus right now is to configure it so that whenever an operator button is clicked after entering a number, it will store both the number ...

What is the best method for integrating both Jquery and Angularjs into a single project?

As we prepare to incorporate Jquery and AngularJs into our upcoming project, I've come across conflicting information. Some sources suggest using Jquery before AngularJs, while others recommend the opposite. This has left me a bit perplexed. For exam ...

Tips for modifying the color of a 3D object using the THREE.SceneUtils.createMultiMaterialObject function

My current project involves an object that has a unique combination of color and wireframe. I am looking to modify the base color and mesh material of this object in its final form. To achieve this, I have decided to utilize MeshBasicMaterial for the Mul ...

Integrating Watson Conversation with Oracle Database

Hello everyone, I am currently working on a project where I need Watson to fetch a response manually set from our Oracle Databases. To achieve this, I am using async to access the database sequentially and return the response. Initially, I faced an issue ...

Looping through objects and updating state based on a filter condition

Within my React state, there exists an array of objects similar to the following: timers: { id:"-LL-YVYNC_BGirSn1Ydu" title: "Project Title" project: "Project Name", elapsed: 0, runningSince: 0, }, { id:"-LL-YVYNC_BGirSn1Ydu-2", ...

Utilizing Javascript to set the innerHTML as the value of a form

I have written a JavaScript code that utilizes the innerHTML function, as shown below: <script> var x = document.getElementById("gps"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showP ...

How can I transfer data to a different component in Angular 11 that is not directly related?

Within the home component, there is a line that reads ...<app-root [message]="hii"> which opens the app-root component. The app-root component has an @input and {{message}} in the HTML is functioning properly. However, instead of opening t ...

The data from my API is not showing up in my React application

Im working on a project where I am trying to retrieve an image of a recipe card from and display it on my react.js application. However, I am encountering difficulties in getting the API data to show up on the page when running the code. Any assistance wo ...

Issue with missing localStorage data

After saving the token in localStorage, I implemented a method to verify its correctness. Here is an excerpt from the code: let token = localStorage.getItem('token'); console.log(token); if ( !token ) { return null; } const parts = token.sp ...

Retrieve various objects from separate files using Node.js

Throughout the development of my Node.js project, which I have been working on since my teenage years and consists of a multitude of files, I have initialized numerous objects in the index.js file. This is also where all function calls originate. In order ...

Having trouble getting into my own Hook with Next.js to access the localStorage

My goal is to create a hook that can store User credentials such as authToken. I attempted to save the user data to localStorage by creating a custom hook for this purpose. Unfortunately, when compiling my code, I encountered an error stating that window i ...

The perplexing nature of the "this" keyword in JavaScript

Original Code - Version 1: const myobj = { a1 : 10, b1 : 20, c1 : 30, myfunc1 :function() { console.log(this) const myfunc2 = function () { let a2 =100 console.log(this) } myfunc2() } ...

Enhancing Interfaces with TypeScript Parameters

Currently, I have simplified interfaces that I combine to create various types: interface MyObject { values: { [1]: number, } } interface MyOtherObject { values: { [2]: number, } } export type MyObjectType = MyObject & ...

You cannot click on a React subcomponent

I am currently working on implementing a "Title" component within a header. The idea is that when you click on the title component, a header will appear with various buttons for formatting options such as bold and underline. This concept was inspired by a ...

Adobe Acrobat does not run JavaScript code in documents that are submitted for electronic signatures

Lately, I have been experiencing issues with pdfs that contain embedded javascript. The strange thing is that the javascript functions perfectly in the Acrobat Pro DC app but fails to execute when the document is sent for signature. To troubleshoot, I cre ...

Modify the getAttribute value if it is null

I'm currently working on a project where I need to compile the answers selected in a quiz into a document. However, I've encountered a roadblock when certain questions are skipped based on previous responses. This leads to an error message: Un ...