How to call php function on java 5 seconds before end sound

how to call php function on java 5 seconds before end sound ?

My code work but it’s for end of sound.

<script type="text/javascript">
					
					function play1(){
					
					var audio = new Audio("<?php echo $play ?>");
					audio.onended = function () {
						document.getElementById("myCheck").click();
					  }  
					audio.play();	
					}
				</script>
				<script type="text/javascript">
						play1();
					</script>

Well which is it? Does your code? (or does your code -not- work?)

Either way I dont think you’ll be calling any ‘local’ PHP function as requested, as PHP is a server side scripting language, where as javascript is a front end based scripting language. (meaning executed -after- the PHP stuff renders/outputs)

Depending on what you want done, you may make an AJAX call to external PHP script to do -something-… and then return -whatever- and then have the javascript/AJAX routine update/handle the response as you desire.

However. I think the main issues or at least first obstacle is going to be you detecting that ‘5 seconds’ before your audio clip ends. correct? (unless I’m reading things wrong?)

While this can certainly be done…it might not be very efficient, as you arent just using a built called back function that executes when the audio ends … you want to do -something- 5 seconds before hand. Which means you (very often) checking the state (currentTime) against the total length (duration) of the audio clip/object. (make sense?)

While this isnt exactly what you need/set up for your project. You should be able to check this out and pick out the key ‘ingredients’ that adapt for your needs:

I was initially thinking, you would be using setInterval() to check over and over the current states… but the more I think about it… perhaps using setTimeout() instead of a param of the duration - 5 seconds would work instead? (havent played with it myself…to be fair)

Actually here is an example what you want to do (I believe) (ignore the extra buttons after the intial ‘play button’… as they are just for extra info/learning.

https://jsfiddle.net/ej37bsgw/1/

Good luck.

Sponsor our Newsletter | Privacy Policy | Terms of Service