Here is a typing animation JS, and here are two function work correctly type()
function when we click type button and the erase()
function when we click on Erase button.
But now i want to create here a new function that is Erase and Retype
means simply i want - When the sentences typed completely and then if we click Erase and Retype
button then it should be erase and retype once.
var captionLength = 0;
var caption = '';
$(document).ready(function () {
captionEl = $('#caption');
$('#test-typing').click(function () {
testTypingEffect();
});
$('#test-erasing').click(function () {
testErasingEffect();
});
});
function testTypingEffect() {
caption = $("#qqq").html();
type();
}
function type() {
captionEl.html(caption.substr(0, captionLength++));
if (captionLength < caption.length + 1) {
setTimeout('type()', 5);
} else {
captionLength = 0;
caption = '';
}
}
function testErasingEffect() {
caption = captionEl.html();
captionLength = caption.length;
if (captionLength > 0) {
erase();
} else {
$('#caption').html("You didn't write anything to erase, but that's ok!");
setTimeout('testErasingEffect()', 1000);
}
}
function erase() {
captionEl.html(caption.substr(0, captionLength--));
if (captionLength >= 0) {
setTimeout('erase()', 5);
} else {
captionLength = 0;
caption = '';
}
}
<div id="qqq">this is some example sentence which I want to </div>
<input type="button" id="test-typing" value=" Type" />
<input type="button" id="test-erasing" value="Erase" />
<input type="button" id="test-erase-and-retype" value="Erase & Retype" />
<br><br>
<span id="caption"></span>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
i tried to this help, but i didn't get success can i know plz what i should to do.
i have already tried this code:
using callback:
$('#test-erase-and-retype').click(function () {
testErasingEffect(function () {
testTypingEffect();
});
});
using when and then condition:
$('#test-erase-and-retype').click(function () {
$.when(testErasingEffect()).then(testTypingEffect());
});
and this
$('#test-erase-and-retype').click(function () {
function firstFunction(){
testErasingEffect();
}
function secondFunction(){
testTypingEffect();
}
firstFunction();
});
But all that not working, this only erase text not Re-Type.
Can i know what mistake am making ?