-1

there is javascript call function

myfunction();

I used

 $('#chap1').on('click', function(){
          audioplay(); 
    });

so the audio function run, I wonder something can "off/on" the function without calling a new function to stop the audio.

Is there any way to stop/close the javascript function?

Elvinci Chen
  • 109
  • 1
  • 6
  • 3
    I'm afraid your question lacks precision - can you elaborate please? – obscure Mar 20 '19 at 20:53
  • 1
    if I understand your question, this answer may apply: https://stackoverflow.com/a/3330206/9533368 – egnomerator Mar 20 '19 at 20:54
  • 2
    Only from within the function - it immediately ends when it reaches a `return` statement. Although this sounds like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/66378#66378) - what is the real issue you try to solve? – VLAZ Mar 20 '19 at 20:54
  • 1
    Please share your function codeblock. What are you trying to achieve? You may need to write a condition or use a method like setTimeout() within your function to determine when it ends. – usrrname Mar 20 '19 at 21:01
  • sorry my mistake, I updated/edited my question – Elvinci Chen Mar 20 '19 at 21:10

2 Answers2

1

I'm not sure what you mean by stop/close the function. If you mean to break out of the function, you can just use return. For example,

function myfunction() {
     if (condition) 
          return;
}

this will stop the function when the condition is met.

Isaac
  • 273
  • 3
  • 19
0

These posts may help with your logic:

I am trying to make a simple toggle button in javascript

Toggle on/off buttons in Javascript

Save your state in a variable that's true or false, then specify opposing conditions in your function.

ie.

//start off not playing as the default state when page loads var isPlaying = false;

 $('#chap1').on('click', function(){
if (isPlaying == false){
          audioplay(); 
}
if (isPlaying == true){
 !audioPlay();
    }
});

This above is pseudo-code. You may have to write a function to stop the playing because I can't see your audioplay() function.

usrrname
  • 328
  • 2
  • 10