0

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>​
4
  • 3
    Do you have any good reason to replace this simple code snippet with jQuery...? Using jQuery doesn't make a code more awesome. Your current code uses well-supported methods. There is absolutely no need to use jQuery. Commented Apr 6, 2012 at 9:40
  • 4
    JavaScript is a programming language, jQuery a web browser framework (which itself is programmed in JavaScript). jQuery is not a language. Apart from that, your DST calculation is wrong. Commented Apr 6, 2012 at 9:41
  • Why you need change it beutiful code? :-) Commented Apr 6, 2012 at 9:43
  • what's the advantage of using jQeury you expected? Commented Apr 6, 2012 at 9:51

3 Answers 3

3

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).

Sign up to request clarification or add additional context in comments.

1 Comment

@jenniferJolie: That's because you're loading jQuery using onload, but running your code earlier. If you load jQuery early enough that your code doesn't try to use it before it's there (or conversely set off your code later), it works just fine: jsfiddle.net/zJRDC/2
1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.