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

  • Home
  • SEARCH
  • 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 7594529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:26:21+00:00 2026-05-30T21:26:21+00:00

I have a image dimensions calculation function which returns scaled image dimension values when

  • 0

I have a image dimensions calculation function which returns scaled image dimension values when it’s executed. (note: the list of images and their dimensions are accessed from the arrays.)

function setDesiredDimensions() {
                var width = Math.min(imagesOrigWidths[currindex], desiredWidthLimit);
                var height = Math.ceil((width / imagesOrigWidths[currindex]) * imagesOrigHeights[currindex]);

                some more calculation code here...

                return {width:width,height:height};
            }     

var size = setDesiredDimensions(imagesOrigWidths[currindex], imagesOrigHeights[currindex]);

Then I have some buttons in HTML:

<a id="button1"></a>
<a id="button2"></a>
<a id="button3"></a>

And various onclick events on these buttons, such as:

$('#button1').click( function() {
     currindex = (currindex+1) % max;

     **I need to evaluate setDesiredDimensions function here **

     $("#imageswap").attr({src: imgSrcBase(imagesGuids[currindex]), width: size.width, height: size.height})
});

The buttons adjust the current image index in the array which I need to apply the dimension calculation on. BUT: I don’t want to have the same setDesiredDimensions function copied and pasted in all button click functions, but rather just access/evaluate it as a shortcut for a cleaner code.

I heard eval(); is dangerous and slow. 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-05-30T21:26:22+00:00Added an answer on May 30, 2026 at 9:26 pm

    If you have define setDesiredDimensions in a scope accessible by all event handlers, you can just call the function. That’s what functions are for.

    I think your problem is that the function is working with global variables instead of arguments passed to it. In you example, you are passing imagesOrigWidths[currindex] as argument but also access imagesOrigWidths[currindex] inside the function, which does not make sense.

    Redefine it so that you can simply pass the arguments it needs, something like

    function setDesiredDimensions(orig_width, orig_height, limit) {
        var width = Math.min(orig_width, limit);
        var height = Math.ceil((width / orig_height) * orig_height);
    
        // some more calculation code here...
    
        return {width:width,height:height};
    }    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an image gallery which loads a detail image, then draws a line
I have an image with 595x842px dimensions, I would like to use the image
On my web site, I have the following code: <input type=image src=images/btn.png alt=Submit border=0
I have a game which displays a full screen image in the background. At
I have a PHP image upload system which allows a user to upload various
I need to display 2d images in opengl using textures. The image dimensions are
I need the ability to put the processed image's dimensions. I have in my
Let's say I have an image called hello.png with dimensions 200x100 . I create
I have a very large array of floating point image data: In[25]:= Dimensions[daylightImgd] Out[25]=
I have an image in a userform which displays a pre-existing chart, according the

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.