2

I'm trying to build an analog clock.

second_hand = document.getElementById('second_hand');

I have a function that get the time.

function getTime() {
  var date = new Date(),
  second = date.getSeconds();

  return second;
}

Then I have a function that rotate the hand.

function rotateHand(hand) {
  var second = getTime();
  hand.style.transformOrigin = "0 100%";
  hand.style.transform = 'rotate(' + second * 6 + 'deg)';
}

Then I am using a setInterval to update my rotateHand() every second.

setInterval(rotateHand(second_hand), 1000); 

But my hand is not updating(moving) every second. What is wrong?

Here is a version of this:

second_hand = document.getElementById('second_hand');
function getTime() {
  var date = new Date(),
  second = date.getSeconds();

  return second;
}
function rotateHand(hand) {
  var second = getTime();
  hand.style.transformOrigin = "0 100%";
  hand.style.transform = 'rotate(' + second * 6 + 'deg)';
}

setInterval(rotateHand(second_hand), 1000);
<div id="second_hand">hiiiiiiiiii</div>

0

3 Answers 3

7

setInterval needs a function reference as the first parameter. You're not passing a function reference, you're invoking the rotateHand function.

You can either :

  • pass a reference to an anonymous function that will call rotateHand with the secondHand parameter : setInterval(function () { rotateHand(second_hand)}, 1000);

  • use Function.prototype.bind to pass a function reference to rotateHand with secondHand as a parameter :

var second_hand = document.getElementById('second_hand');

function getTime() {
  var date = new Date(),
  second = date.getSeconds();

  return second;
}

function rotateHand(hand) {
  var second = getTime();
  hand.style.transformOrigin = "0 100%";
  hand.style.transform = 'rotate(' + second * 6 + 'deg)';
}

setInterval(rotateHand.bind(null, second_hand), 1000);
<div id="second_hand">2nd hand</div>

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

6 Comments

oh hey i guess we both follow the javascript tag
Thanks, that did the trick! Strangely, I was testing my setInterval with another function and it was working while I was passing only the function, not a reference! Any idea why?
Doesn't call run the function immediately, wouldn't it make more sense to use bind?
@FredStark bind is what I meant. Thanks for pointing that out. I edited.
I'm more surprised call worked for the OP! haha
|
2

The issue probably has to do with this line:

setInterval(rotateHand(second_hand), 1000); 

rotateHand(second_hand) evaluates to undefined. So you're setInterval isn't doing anything.

Instead try this:

setInterval(()=>rotateHand(second_hand), 1000); 

Or the equivalent without arrow sugar:

setInterval(function(){rotatehand(second_hand)},1000);

2 Comments

That worked too. I'm not familiar with how the arrow expression works though. Thanks!
I've added an example without them for you.
0

Use this, it should work:

function(){rotateHand(second_hand);}

rotateHand(second_hand); isn't a function reference so it won't work.

second_hand = document.getElementById('second_hand');
function getTime() {
  var date = new Date(),
  second = date.getSeconds();

  return second;
}
function rotateHand(hand) {
  var second = getTime();
  hand.style.transformOrigin = "0 100%";
  hand.style.transform = 'rotate(' + second * 6 + 'deg)';
}

setInterval(function(){rotateHand(second_hand);}, 1000);
<div id="second_hand">------------</div>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.