below i got some code, which should delay 2 seconds and hide previous span and show the next span, but i cant seem to get it to work.
<style>
span{ display:none}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"><script>
<div id="text"></div>
<script>
var text = [
"<span>Sent A blabla</span>",
"<span>sent B</span><span>sent b bbb cont....</span>",
"<span>Sent c...</span><span>Sent C2</span><span>sent c3</span>", ];
var text = text[Math.floor(Math.random() * text.length)];
jQuery('#text').html(text);
jQuery('#text').show();
jQuery('#text span:first').show();
var delay = 2000;
jQuery("#text span").each(function() {
setTimeout( function(){
var el = $(this);
el.prev().hide();
el.show();
}, delay)
delay += 2000;
});
</script>
The problem is occurring because of your use of the keyword
this
.Inside the for each statement you need to set a variable that is set to $(this) and then reference that variable in the timeout. Example below:
JS Fiddle: http://jsfiddle.net/Lykwmfz8/