Why is jquery script taking forever?
<p>I am new to jquery and running a script on my homepage that determines the number of lines in a container (i.e. div).  If the number of lines is over 3 or 4 lines (depending on whether div class is "threeline" or "fourline"), it will truncate it and add an ellipsis.  However, this script is taking forever to execute so I'm wondering why or how it can be optimized.  I've tried to wrap my head around javascript/jquery, but it truly is a foreign language to me!</p>

<p>Can someone please take a look at this script and let me know how I can edit it to make it faster?  Is it unnecessarily checking all elements on the page?</p>

<p>Here is how it is called on my php page:</p>

<pre><code>    &lt;div class="bubble left"&gt;
    &lt;div class="rounded"&gt;
            &lt;blockquote class="ellipsis fourline"&gt;
                    &lt;h3&gt;&lt;a href="http://www.xyz.com"&gt;This is the title&lt;/a&gt;&lt;/h3&gt;

                    &lt;p&gt;This is the body of the quote; if the body plus title is more than fourlines, then the text will be truncated and an ellipsis will be added&lt;/p&gt;
            &lt;/blockquote&gt;
    &lt;/div&gt;
    &lt;cite class="rounded"&gt;March 18, 2010&lt;/cite&gt;
    &lt;/div&gt;


&lt;script type="text/javascript" src="/scripts/js/jquery.ellipsis.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt; $(".ellipsis").ellipsis(); &lt;/script&gt;
</code></pre>

<p>And this is the jquery.ellipsis.js file:</p>

<pre><code>(function($) {
        $.fn.ellipsis = function()
        {
                return this.each(function()
                {
                        var el = $(this);

                        if(el.css("overflow") == "hidden" &amp;&amp; el.hasClass('fourline'))
                        {
                                var text = el.html();
                                var fourline = el.hasClass('fourline');
                                var t = $(this.cloneNode(true))
                                        .hide()
                                        .css('position', 'absolute')
                                        .css('overflow', 'visible')
                                        .width(fourline ? el.width() : 'auto')
                                        .height(fourline ? 'auto' : el.height())
                                        ;

                                el.after(t);

                                function height() { return t.height() &gt; el.height(); };
                                function width() { return t.width() &gt; el.width(); };

                                var func = fourline ? height : width;

                                while (text.length &gt; 0 &amp;&amp; func())
                                {
                                        text = text.substr(0, text.length - 1);
                                        t.html(text + "&amp;hellip;");
                                }

                                el.html(t.html());
                                t.remove();
                        } else if (el.css("overflow") == "hidden" &amp;&amp; el.hasClass('threeline'))
                        {
                                var text = el.html();
                                var threeline = el.hasClass('threeline');
                                var t = $(this.cloneNode(true))
                                        .hide()
                                        .css('position', 'absolute')
                                        .css('overflow', 'visible')
                                        .width(threeline ? el.width() : 'auto')
                                        .height(threeline ? 'auto' : el.height())
                                        ;

                                el.after(t);

                                function height() { return t.height() &gt; el.height(); };
                                function width() { return t.width() &gt; el.width(); };

                                var func = threeline ? height : width;

                                while (text.length &gt; 0 &amp;&amp; func())
                                {
                                        text = text.substr(0, text.length - 1);
                                        t.html(text + "&amp;hellip;");
                                }

                                el.html(t.html());
                                t.remove();
                        }
                });
        };
})(jQuery);
</code></pre>

<p>Thanks in advance, I love this site!</p>
