Total Pageviews

Sunday, March 3, 2013

jQuery Type Testing Function

Type testing function

  • Determine the type of an object
  • Useful for optional parameters & validation
Example
             In this example  isNumeric() and isFunction() are used as Type testing function
   
   <head>
    <title></title>
    <script src="script/jquery-1.7.1.js" type="text/javascript"></script>

</head>
<body>
    <script type="text/javascript">
        function callAnotherFunction(arg1,arg2,arg3) {
            var times = $.isNumeric(arg1) ? arg1 : 3;
            var delay = $.isNumeric(arg2) ? arg2 : 1000;
            var functiontocall = $.isFunction(arg1) ? arg1 : $.isFunction(arg2) ? arg2 : arg3;
            var i = 0;
            (function loopIt() {

                i++;
                functiontocall();
                if(i<times) {
                    setTimeout(loopIt, delay);
                }
            })();
        }        
   

        function functionToCall() {
            $('#output').append("<br/>fuction called");
        }

        $(document).ready(function () {
            callAnotherFunction(3, 500, functionToCall);
        });
               
    </script>
    <div id="output"></div>
</body>
</html>

   
   
Output
fuction called
fuction called
fuction called

No comments:

Post a Comment