Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2006
    Posts
    93
    Thanks
    3
    Thanked 0 Times in 0 Posts

    call function after idle of ? seconds

    instead of calling a function this way

    Code:
    <input onkeyup="Select(this,'employe');" size="5">
    is it possible to call the function after the field has not been changed in 1 or 2 seconds?

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    <input onchange="setTimeout(function(){Select(this,'employe');},2000);" size="5">

    not tested, might not work. but you get the idea, I hope.

  • #3
    New Coder
    Join Date
    Mar 2006
    Posts
    93
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Beagle
    <input onchange="setTimeout(function(){Select(this,'employe');},2000);" size="5">

    not tested, might not work. but you get the idea, I hope.
    Anyone know how come this returns an undefined error?
    Code:
    <input onkeyup="setTimeout(function(){Select(this,'employe')},2000);" size="5">

  • #4
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    test in Firefox and use their javascript console to get more meaningful errors than the crap IE spews.

  • #5
    New Coder
    Join Date
    May 2006
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <input onkeyup="setTimeout(function(){Select(this,'employe')},2000);" size="5">
    What has happened is, the variable "this" no longer refers to the input htmlElement Object when the setTimeout() function is executed after 2 seconds. Instead "this" refers to the "window" object, since setTimeout() is always executed in the scope of the "window".

    To preserve the reference to the input Object, you can use the following:

    Code:
    <input onkeyup="var me = this; (function() { setTimeout(function(){Select(me, 'employe')}, 2000); })();" size="5">
    This is the form of writing:

    Code:
    <input onkeyup="selectTimeout(this);" size="5">
    
    
    <script type="text/javascript">
    <!--
    
    function selectTimeout(me) {
    	setTimeout(function(){Select(me, 'employe')}, 2000);
    }
    
    function Select(me, var2) {
    	alert(me);
    /* your Select code */
    }
    
    //--></script>
    withought creating a named function, instead the first one uses anonymous functions.
    How both work is that they create a copy of the "this" var, which refers to the htmlElement Input Object, to the "me" var.
    The "me" var creates a closure so that the Input Object is available to the Select function later on when its called by setTimeout.
    Fiji Web Design - where i do Joomla Web Design
    Bucabay.com - My blog


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •