Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to get handler from <code>setTimeout()</code> and run <code>clearTimeout()</code> with the handler.</p> <pre><code>var handler = setTimeout(function () { typeString($target, str, cursor + 1, delay, cb); }, delay); .... clearTimeout(handler); </code></pre> <p>Edit: I modified your code to explain where you should put a stop routine.</p> <p>Check this link: <a href="http://jsbin.com/enukit/1" rel="nofollow">http://jsbin.com/enukit/1</a></p> <p>You need to clean your logic little more. Cursor should be separated or set by option, because you meant to stop the loop, which is used to blink cursor.</p> <pre><code>(function ($) { // writes the string // // @param jQuery $target // @param String str // @param Numeric cursor // @param Numeric delay // @param Function cb // @return void var handler1, mycount=0, handler2, handler3; function typeString($target, str, cursor, delay, cb) { $target.html(function (_, html) { return html + str[cursor]; }); //alert('cursor:'+cursor); //alert('str.length:'+str); if (cursor &lt; str.length - 1) { handler1 = setTimeout(function () { typeString($target, str, cursor + 1, delay, cb); }, delay); } else { cb(); } } // clears the string // // @param jQuery $target // @param Numeric delay // @param Function cb // @return void function deleteString($target, delay, cb) { var length; $target.html(function (_, html) { length = html.length; return html.substr(0, length - 1); }); if (length &gt; 1) { handler2 = setTimeout(function () { deleteString($target, delay, cb); }, delay); } else { cb(); } } // jQuery hook $.fn.extend({ teletype: function (opts) { var settings = $.extend({}, $.teletype.defaults, opts); $(this).each(function () { mycount=0; (function loop($tar, idx) { mycount ++; // type typeString($tar, settings.text[idx], 0, settings.delay, function () { // delete clearTimeout(handler1); handler3 = setTimeout(function () { deleteString($tar, settings.delay, function () { if (mycount&lt;settings.text.length) { loop($tar, (idx + 1));} else { clearTimeout(handler3); alert('The end!'); } }); }, settings.pause); }); }($(this), 0)); }); } }); // plugin defaults $.extend({ teletype: { defaults: { delay: 100, pause: 5000, text: [] } } }); }(jQuery)); $('#target').teletype({ text: [ 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr,', 'sed diam nonumy eirmod tempor invidunt ut labore et dolore', 'magna aliquyam erat, sed diam voluptua. At vero eos et' ] }); /* $('#cursor').teletype({ text: ['_', ' '], delay: 0, pause: 500 }); */ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload