Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use different clocks. #3281

Closed
wotta opened this issue Jul 4, 2016 · 4 comments
Closed

Cannot use different clocks. #3281

wotta opened this issue Jul 4, 2016 · 4 comments

Comments

@wotta
Copy link

wotta commented Jul 4, 2016

Hello,

I am trying to use the clock that shows on the homepage of momentjs.com to show twice with different timezones.
But my code is not working since it only shows one timezone.

Can somebody please show how to fix this.

Here is my code :

<span class="pull-left">
    <div class="hero-circle">
        <div class="hero-face">
            <div id="hour" class="hero-hour"></div>
            <div id="minute" class="hero-minute"></div>
            <div id="second" class="hero-second"></div>
        </div>
    </div>                                    
</span>

<span class="pull-right">
    <div class="hero-circle">
        <div class="hero-face">
            <div id="hour2" class="hero-hour"></div>
            <div id="minute2" class="hero-minute"></div>
            <div id="second2" class="hero-second"></div>
        </div>
    </div>
</span>
var currentLang = 'en',
    snippets = [];

function updateSnippets () {
    var i;

    moment.locale(currentLang);

    for (i = 0; i < snippets.length; i++) {
        snippets[i].render();
    }
}

function updateClock(timezone = "Europe/Amsterdam", selector = ""){
    var now = moment(),
        second = now.seconds() * 6,
        minute = now.minutes() * 6 + second / 60,
        hour = ((now.hours() % 12) / 12) * 360 + 90 + minute / 12;
    now.tz(timezone);

    $('#hour'+selector).css("transform", "rotate(" + hour + "deg)");
    $('#minute'+selector).css("transform", "rotate(" + minute + "deg)");
    $('#second'+selector).css("transform", "rotate(" + second + "deg)");
}

function spaces (length) {
    var out = "";
    while (out.length < length) {
        out += " ";
    }
    return out;
}

function Snippet (el) {
    var longest = 0,
        i,
        text  = this.text  = el.text().split('\n'),
        html  = this.html  = el.html().split('\n'),
        evals = this.evals = [];

    this.el = el;

    for (i = 0; i < text.length; i++) {
        longest = Math.max(text[i].length, longest);
        evals[i] = new Function('return ' + text[i]);
    }

    for (i = 0; i < text.length; i++) {
        html[i] += spaces(longest - text[i].length);
    }
}

Snippet.prototype.render = function () {
    var output = [],
        i;

    for (i = 0; i < this.html.length; i++) {
        output[i] = this.html[i];
        output[i] += '<span class="comment"> // ';
        output[i] += this.evals[i]();
        output[i] += '</span>';
    }

    this.el.html(output.join('\n'));
};


function timedUpdate () {
    updateClock();
    updateClock("America/Curacao", "2");
    updateSnippets();
    setTimeout(timedUpdate, 1000);
}

$('.page-moment-index code').each(function () {
    snippets.push(new Snippet($(this)));
});

timedUpdate();

$(document).on('click', '[data-locale]', function(){
    var dom = $(this);
    currentLang = dom.data('locale');
    $('[data-locale]').removeClass('active');
    dom.addClass('active');
    updateSnippets();
});
@icambron
Copy link
Member

icambron commented Jul 5, 2016

This is more of a question for StackOverflow. If you can narrow your issue down to a few lines of JS that demonstrate a call to Moment, what you expect it to return, and what it actually returns, we can help. But this isn't the right forum for seeking debugging help for your jQuery code.

@icambron icambron closed this as completed Jul 5, 2016
@wotta
Copy link
Author

wotta commented Jul 5, 2016

Well I expected 2 different timezones to be used with 2 clocks.
So one for each but it is doing one for both.

But thanks for closing without waiting for a reply.
And yes I get your not a support forum.

@icambron
Copy link
Member

@concept-core Well, look, it's like a hundred lines of code for generating HTML. Bug reports look like this:

moment("2016-05-25T09:00").something(); //returned 8, expected 9

Until you have something of that form, you haven't provided me with something I can help you with. I'd have to debug your code, figure out exactly what it expects of Moment, figure out how its behavior differs from that, and write a real bug report for you. For obvious reasons, I'm not going to do that, and frankly it's weird that you'd expect me to.

Isolating a bug can be hard work. But it's your hard work, not mine.

@wotta
Copy link
Author

wotta commented Jul 12, 2016

I understand but next time it would be helpfull to not close directly...
The problem on the other hand was that I did not call .tz on the moment() function.

Thanks anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants