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 8872905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:19:56+00:00 2026-06-14T18:19:56+00:00

I read this question here Is it faster to swap an img src or

  • 0

I read this question here “Is it faster to swap an img src or show/hide multiple images?”

The answer to the question has two options.

1) Alter the image source – Page loads faster, swapping consumes time.

2) Pre load images and then simply show/hide – Page loads a little slower, swapping is quicker

To my problem, I go with the second option as load time is 2nd priority for me. But is there best or most optimal way of writing the code?

Let’s say I keep getting numbers (10,11,15,25,13,19 etc randomly) from somewhere. I have to show that many images (small dots, 30 in number, each image about 1kb).. I also have the condition, every dot represents 1.5. So I have written the following code.

var dots = new Array(30);

function onBodyLoad() {
    for(var j=0;j<30;j++)
        dots[j] = document.getElementById("dotimg"+j);
}

//called by some method every second.
//argument: the value received from somewhere.

function updateImages(s) {
    var x = Math.round(s);
    var dotPos = x/1.5;
    for(var i=0;i<dotPos;i++) {
        dots[i].style.visibility='visible'; //show that many dots
        document.getElementById('dot-val').textContent=s;//value received shown in span
    }
    for(var j=dotPos;j<30;j++) dots[j].style.visibility='hidden';//hide remaining dots
}

So if the value received is 25 in one second, 17 dots will be shown.. if in the next second the value received is 15, 10 dots will be shown, the rest will be hidden. Is there a better way of writing the above code?

  • 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-14T18:19:57+00:00Added an answer on June 14, 2026 at 6:19 pm

    First of all, move the 'dot-val' setting out of the for loop (You’re assigning the same value to it in each iteration of the for loop).
    Also, you can change the display states in 1 loop, saving a for:

    function updateImages(s) {
        var x = Math.round(s);
        var dotPos = x/1.5;
        for(var i=0;i<30;i++) {
            if(i < dotPos){
                dots[i].style.display='inline-block'; // Assuming they're inline-block elements.
            }else{
                dots[i].style.display='none';
            }
        }
        document.getElementById('dot-val').textContent=s;//value received shown in span
    }
    

    Now, if you really want to trim your code, skip the temp x var, and use a Ternary operator:

    function updateImages(s) {
        var dotPos = Math.round(s)/1.5;
        for(var i=0;i<30;i++) {
            dots[i].style.display = (i < dotPos)? 'inline-block' : 'none';
        }
        document.getElementById('dot-val').textContent = s;//value received shown in span
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched here, GooBingHooVista'd the world and read this related question for VS
This is a follow-up to my other question here: How to read vfat attributes
I read this question in stackoverflow. The excerpt answer provided by bbum is below:
I read this question but the answer does not perform a refactoring of the
I have read this question and the simple and clear answer but it's not
I know this question has been asked before and I read all the answers
There are some other variations of this question here at SO, but please read
I recently read the question here Why is it faster to process a sorted
Having read this question Immutable or not immutable? and reading answers to my previous
I have read this question but it's not quite what I was looking for.

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.