Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8397409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:46:57+00:00 2026-06-09T20:46:57+00:00

Simple while loop is not working properly. I write a script for limiting the

  • 0

Simple while loop is not working properly.
I write a script for limiting the text to 3 lines. It’s very simple. I check if the height of the element (div) is higher than 3 lines (hardcoded font-size and line-height) and if so, I remove the last word and add ellipses. And I am doing it while string is shortly enough to fit on 3 lines.

It is working randomly, sometimes works, sometimes doesn’t. If not working I have only 1 word on the 4th line. So it skips only the final iteration and not every time.

$('div.prod-description').each(function(){
    var obj = $(this);

    if(obj.height() > 60) { // line-height: 18px;
        var ellipses = '...';
        var new_text = null;
        while(obj.height() > 60) {
            new_text = obj.text().slice(0, obj.text().lastIndexOf(' '));
            obj.text(new_text+ellipses);
        }
    }
});

I tried to remove the if statement, I tried with do while statement but it’s the same.

EDIT: I found the problem! But don’t know how to fix it. If I hit enter on the browser’s address bar or I come to page from the menu it’s working properly. If I refresh with F5 it’s skipping the last iteration. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:46:58+00:00Added an answer on June 9, 2026 at 8:46 pm

    After I was lucky to find that the script doesn’t work properly only on refreshing the page with F5 I found the solution.
    https://stackoverflow.com/a/8310308/1333512

    To correct this problem use:

    $(window).load(function() {});
    

    Out instead of:

    $(document).ready(function() {});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a simple Stored Procedure with a while loop that's not working. Here's
I have a script that uses a simple while loop to display a progress
While working on a simple programming exercise, I produced a while loop (DO loop
Its very simple but somehow not working (weird!!). I have a List of a
I have a simple working PHP script to write an HTML table from a
i'm working on a simple loop but it's not working. I have 2 files
Say you have a simple loop while read line do printf ${line#*//}\n done <
I have a simple problem while putting text in multiple table! please have a
I have a simple Lua script: while ( i < 500000 ) do redis.call(zadd,
I'm working on a simple JavaScript game using HTML5 canvas. It's a very typical

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.