jquerytypescripttypescript1.4

jQuery timer using with TypeScript


I'm new with TypeScript so sorry for the beginner question.

I'm trying to create a new JQueryTimer in TypeScript and start it after a keypress. I'm using the DefinitelyTyped jQuery and jQuery.timer definitions.

My app.ts:

/// < reference path='references.ts'/>
module app {
   function addKeyToString(e) {
        $("body").timer(
            function () {
                console.log("This function just got called");
            }, 10000, true
            );
        document.getElementById("typingSpan").innerText += e.char;
    }    
    document.onkeypress = addKeyToString;
}

And my index.html looks like this:

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>TypeScript HTML App</title>


    <script src="lib/jquery/jquery.js"></script>
    <script src="lib/jquery/jquery-ui.min.js"></script> 
    <script type="text/javascript" src="app.js"></script>
    <script src="lib/jquery/jquery.timer.js"></script>

    <link rel="stylesheet" href="app.css" type="text/css" />

</head>
<body>
    <h1>TypeScript HTML App</h1>
    WriteHere: <span id="typingSpan"></span>
</body>

</html>

When I'm trying to start this and press a key I receive runtime error (at the first line of my function) which says:

Object doesn't support property or method 'timer'

Can someone tell me what is the solution to my problem? I checked the DefinitelyTyped example and based on that this should work. Here is the link: Link


Solution

  • You should refer to the documentation for the library, https://github.com/jchavannes/jquery-timer

    This gives the example usage:

    var timer = $.timer(function() {
        alert('This message was sent by a timer.');
    });
    

    Tests for d.ts files are not necessarily going to be correct and shouldn't be used as reference documents. I don't know why this one appears to be wrong.