I want to execute a custom jQuery function after another custom function completes
The first function is used for creating a "typewriting" effect
function Typer()
{
var srcText = 'EXAMPLE ';
var i = 0;
var result = srcText[i];
setInterval(function() {
if(i == srcText.length) {
clearInterval(this);
return;
};
i++;
result += srcText[i].replace("\n", "<br />");
$("#message").html( result);
},
100);
}
and the second function plays a sound
function playBGM()
{
document.getElementById('bgm').play();
}
I am calling the functions one after the another like
Typer();
playBGM();
But the sound starts playing as the text is getting typed. I want to play the sound only AFTER the typewriting has finished.
Here is what I have tried: http://jsfiddle.net/GkUEN/5/
How can I fix this?
function Typer(callback) { var srcText = 'EXAMPLE '; var i = 0; var result = srcText[i]; var interval = setInterval(function() { if(i == srcText. length - 1) { clearInterval(interval); callback(); return; } i++; result += srcText[i]. replace("\n", "<br />"); $("#message").
Simply put: A callback is a function that is to be executed after another function has finished executing — hence the name 'call back'. More complexly put: In JavaScript, functions are objects. Because of this, functions can take functions as arguments, and can be returned by other functions.
function a() { // first function code here $(document). trigger('function_a_complete'); } function b() { // second function code here } $(document). bind('function_a_complete', b); Using this method, function 'b' can only execute AFTER function 'a', as the trigger only exists when function a is finished executing.
function someFunction() { //do stuff } $(document). ready(function(){ //Load City by State $('#billing_state_id'). live('change', someFunction); $('#click_me'). live('click', function() { //do something someFunction(); }); });
You can use below code
$.when( Typer() ).done(function() {
playBGM();
});
You should use a callback parameter:
function Typer(callback)
{
var srcText = 'EXAMPLE ';
var i = 0;
var result = srcText[i];
var interval = setInterval(function() {
if(i == srcText.length - 1) {
clearInterval(interval);
callback();
return;
}
i++;
result += srcText[i].replace("\n", "<br />");
$("#message").html(result);
},
100);
return true;
}
function playBGM () {
alert("Play BGM function");
$('#bgm').get(0).play();
}
Typer(function () {
playBGM();
});
// or one-liner: Typer(playBGM);
So, you pass a function as parameter (callback
) that will be called in that if
before return
.
Also, this is a good article about callbacks.
function Typer(callback)
{
var srcText = 'EXAMPLE ';
var i = 0;
var result = srcText[i];
var interval = setInterval(function() {
if(i == srcText.length - 1) {
clearInterval(interval);
callback();
return;
}
i++;
result += srcText[i].replace("\n", "<br />");
$("#message").html(result);
},
100);
return true;
}
function playBGM () {
alert("Play BGM function");
$('#bgm').get(0).play();
}
Typer(function () {
playBGM();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div id="message">
</div>
<audio id="bgm" src="http://www.freesfx.co.uk/rx2/mp3s/9/10780_1381246351.mp3">
</audio>
JSFIDDLE
You could also use custom events:
function Typer() {
// Some stuff
$(anyDomElement).trigger("myCustomEvent");
}
$(anyDomElement).on("myCustomEvent", function() {
// Some other stuff
});
Deferred promises are a nice way to chain together function execution neatly and easily. Whether AJAX or normal functions, they offer greater flexibility than callbacks, and I've found easier to grasp.
function Typer()
{
var dfd = $.Deferred();
var srcText = 'EXAMPLE ';
var i = 0;
var result = srcText[i];
UPDATE :
////////////////////////////////
var timer= setInterval(function() {
if(i == srcText.length) {
// clearInterval(this);
clearInterval(timer);
////////////////////////////////
dfd.resolve();
};
i++;
result += srcText[i].replace("\n", "<br />");
$("#message").html( result);
},
100);
return dfd.promise();
}
I've modified the play function so it returns a promise when the audio finishes playing, which might be useful to some. The third function fires when sound finishes playing.
function playBGM()
{
var playsound = $.Deferred();
$('#bgm')[0].play();
$("#bgm").on("ended", function() {
playsound.resolve();
});
return playsound.promise();
}
function thirdFunction() {
alert('third function');
}
Now call the whole thing with the following: (be sure to use Jquery 1.9.1 or above as I found that 1.7.2 executes all the functions at once, rather than waiting for each to resolve.)
Typer().then(playBGM).then(thirdFunction);
Before today, I had no luck using deferred promises in this way, and finally have grasped it. Precisely timed, chained interface events occurring exactly when we want them to, including async events, has never been easy. For me at least, I now have it under control thanks largely to others asking questions here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With