jennifer Jolie
jennifer Jolie

Reputation: 727

Change javascript code to jquery code, How is it?

I want change following javascript code to jquery code, How is it?

With done change hourOffset in date 3, 21 and 9, 22.

DEMO: http://jsfiddle.net/zJRDC/

<script type="text/javascript">
    var interval = self.setInterval("clock()", 1000);
    function clock() {
        var date = new Date();
        var hourOffset = 3;
        date.setUTCHours(date.getUTCHours(), date.getUTCMinutes());
        var time = date.getTime();
        date.setUTCFullYear(date.getUTCFullYear(), 3, 21);
        var dstStart = date.getTime();
        date.setUTCFullYear(date.getUTCFullYear(), 9, 22);
        var dstEnd = date.getTime();
        if (time > dstStart && time < dstEnd){ hourOffset = 4;}
        date.setUTCHours(date.getUTCHours() + hourOffset, date.getUTCMinutes() + 30);
        var output = date.getUTCHours() + ":" + date.getUTCMinutes() + ":" + date.getUTCSeconds();
        document.getElementById("clock").innerHTML = output
    }
    </script>
<div id="clock"></div>​

Upvotes: 0

Views: 2830

Answers (3)

aifarfa
aifarfa

Reputation: 3939

if your code works. you don't really need jquery. unless you want to create re-usable function or your custom plugin.

a quick sample to use clock() as jquery plugins (didn't test)

(function( $ ){

  $.fn.myClock = function(timeout) {

    var interval = setInterval("clock()", timeout);

    function clock() {
        //..calculate date output
        var output = //...
        this.html(output)
    }
  };
})( jQuery );

then to use your plugin

$(document).ready(function(){
  $('#clock').myClock(1000);
}

see Plugins/Authoring and learn-how-to-create-your-own-jquery-plugin

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074088

There's precisely one line of that where jQuery might apply:

document.getElementById("clock").innerHTML = output

which with jQuery could be

$("#clock").html(output);

That uses $ to look up the element, which includes a workaround for a bug in getElementById in earlier versions of IE, and then uses the html method to set the markup on the element (which is very like setting innerHTML — again, though, with some workarounds for problematic bits in some cases; they probably don't apply to this specific code).


Note that jQuery is just a framework written in JavaScript. It smooths over some browser differences dealing with the DOM, and provides a lot of handy utility functionality, but it's not a thing separate from JavaScript. You write JavaScript code, and you optionally use jQuery in it (just like any other library).

Upvotes: 3

Biard K&#233;vin
Biard K&#233;vin

Reputation: 137

JQuery is already done with JavaScript . Nothing to convert because JQuery is not a language ;).

Upvotes: 0

Related Questions