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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:17:01+00:00 2026-06-02T01:17:01+00:00

Let’s say that I want to invoke a function with the onClick method. Like

  • 0

Let’s say that I want to invoke a function with the onClick method. Like this:

<li class="inline" onclick="mve(this);" >TEMP</li>

AND I have a JS function that looks like this:

function mve(caller){
caller.style.position = "relative";
caller.style.left = (caller.style.left+20) +'px';
var foo = setTimeout('mve(caller)', 2000);
}

My problem is that the element (which caller refers to) is undefined after the initial onClick call. At least this is what Firebug is telling me.

I’m sure it’s an easy solution, so how about just a simple explanation of why, and how?

Also if I run it like so:

function mve(caller){
caller.style.position = "relative";
caller.style.left = (caller.style.left+20) +'px';
}

I would think the element would move 20px right on every click, however that is not the case. Thoughts?

  • 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-02T01:17:03+00:00Added an answer on June 2, 2026 at 1:17 am

    setTimeout() executes a string parameter in the global scope so your value of this is no longer present nor is your argument caller. This is one of many reasons NOT to use string parameters with setTimeout. Using an actual javascript function reference like this and it’s quite an easy problem to solve getting the arguments passed accordingly:

    function mve(caller){
        caller.style.position = "relative";
        caller.style.left = (caller.style.left+20) +'px';
        setTimeout(function() {
            mve(caller)
        }, 2000);
    }
    

    For the second part of your question, caller.style.left is going to have units on it like 20px so when you add 20 to it, you get 20px20 and that’s not a value that the browser will understand so nothing happens. You will need to parse the actual number out of it, add 20 to the number, then add the units back on like this:

    function mve(caller){
        caller.style.position = "relative";
        caller.style.left = (parseInt(caller.style.left), 10) +20) + 'px';
        setTimeout(function() {
            mve(caller)
        }, 2000);
    }
    

    Something that is missing from this function is a way for it to stop repeating. As you have it now, it goes on forever. I might suggest passing in either a number of iterations like this:

    function mve(caller, iterationsRemaining){
        caller.style.position = "relative";
        caller.style.left = (parseInt(caller.style.left), 10) +20) + 'px';
        if (--iterationsRemaining) > 0) {
            setTimeout(function() {
                mve(caller, iterationsRemaining)
            }, 2000);
        }
    }
    

    Also, you might be curious to know that this isn’t really a recursive function. That’s because the mve() function calls setTimeout() and then finishes right away. It is setTimeout() that executes the next iteration of mve() some time later and there is no accumulation on the stack frame of multiple function calls and thus no actual recursion. It does look like recursion from a glance at the code, but isn’t technically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I can call a method like this: core::get() . What is the
Let's say that I have a set of relations that looks like this: relations
Let's say I dynamically create a timer like this: System.Timers.Timer expirationTimer = new Timer(expiration
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's say that I have a model that handles recipes, and I want to
Let's say I have this string which I want to put in a multidimensional
Let's say I have table with column 'URL' whrere I store urls like this
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a

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.