This is a simplified and explained version of my code:
var ptext=parr.find('div.ptext'); //Text Container
var pt=ptext.html(); //Text Container's string
var pdv=[pt.split(" ")]; //Text Container's array of words
pdv.push(pdv[0].length); //Write also the number of words
var ht=hp.html(); //Text Container's new string
var hdv=[ht.split(" ")]; //Text Container's new array of words
hdv.push(hdv[0].length); //New number of words
var kakaka=0; //If they begin with the same,
for (var j=0;j<hdv[0].length;j++){ //Animate only from the position
if (hdv[0][j]==pdv[0][j]) //where they differ
kakaka+=2;
}
window.clearTimeout(ptext.data('curt')); //Clear current animation's interval
ptext.data('count',kakaka); //Set starting position
ptext.data('curt', //Set interval's var into object
window.setInterval((function (item,pdv,hdv,text_callback) {
return function() { //item = text obj, text_callback= callback function
var i=item.data('count');
i=(i==undefined)?0:1+i;
item.data('count',i); //increase counter
//first phase - remove old text
if (i<=pdv[1]) // go on
{
item.html((pdv[0].slice(0,pdv[1]-i)).join(' '));
} //if reached the position, add new text
else if (i<=pdv[1]+hdv[1])
{
item.html((hdv[0].slice(0,i-pdv[1])).join(' '));
} //if finish clear timeout and call callback
else {
item.data('count',0);
window.clearTimeout(item.data('curt'));
text_callback();
}
}
})(ptext,pdv,hdv,text_callback),8) //8 = supposed interval
);
}
It gets the words from the div, removes them one by one quickly, and then populates it with the new text.
It uses .setInterval() function, which was supposed to callback every 8ms. This works beautifully on my i5 CPU, but it is terrifically slow on an i3 laptop.
Could you give some advice on how to increase performance?
This is the function that did it perfectly, the whole idea was to wrap it in SPANS, then for each span we would change the visibility, and when it gets to the reverse phase, replace with different text wrapped in spans.
I don’t guarantee you will ever understand anything, but you can surely get an idea how I solved it. Note, basically the main inputs are pdv, and hdv – are the word arrays of input text and output text.