www.webdeveloper.com
Results 1 to 2 of 2

Thread: how call an inner function in javascript

  1. #1
    Join Date
    Dec 2012
    Posts
    1

    how call an inner function in javascript

    i have an inner function and i call it with setTimeout like this
    Code:
    <!DOCTYPE html>
    
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script type="text/javascript">
    
            function Overrideablefunc() {
    
                this.runfunc=function () {
                    document.getElementById('txt').value = Date().substring(0, 24);
                    //***************************************
                    var overrd = new Overrideablefunc()
                    setTimeout(overrd.runfunc, 1000);
                    //***************************************
    
                 }
            }
    
            function runnewfunc() {
                var a=new Overrideablefunc()
                a.runfunc();
            }
    
        </script>
    
    </head>
    <body>
        <input type="button" value="Run" onclick="runnewfunc()"/><br />
        <input type="text" id="txt" style="width:400px"/>
    </body>
    </html>
    i call my function with

    //***************************************
    var overrd = new Overrideablefunc()
    setTimeout(overrd.runfunc, 1000);
    //***************************************

    but i don't know this way is right or not?
    do i have any other ways for call my function?

    thank you

  2. #2
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    Very odd code you have... but, you could return the inner function from the outer function. Example:
    Code:
    function outer() {
        function inner() {
            alert('Hello');
        }
        return inner;
    }
    
    var func = outer();
    setTimeout(func, 2000); // alerts "Hello" after two seconds

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles