# Getting value of clock [duplicate]

I have the following code:

a = Dynamic[Floor[Clock[2]]] (*So this basically alternates between 0 and 1*)
If[a==1, Print["true"]]


But this doesn't work, it instead prints the following If[a==0, Print["true"]] and then changes to If[a==1, Print["true"]] and then back.

How can I get the value from the clock?

-

## marked as duplicate by Mr.Wizard♦Apr 8 '13 at 10:22

– Mr.Wizard Apr 8 '13 at 10:25

Well, looking at the other answers, I am not sure if I correctly understand what you want. But I'll give it a go:

a = Dynamic[Floor[Clock[2]]](*same thing*)
Dynamic[If[Setting@a == 1, "true", "false"]]


Does this yield what you wanted? The problem in your case is, that a is no "plain" integer, see FullForm[a]. Thus, the test a==1 is not executing. Using Setting lets you retrieve the value of a Dynamic var, see e.g. here: Dynamic Values

-
Thank you very much, this works! – user1626227 Apr 8 '13 at 8:11

Well, the problem locates in the difference between Equal and SameQ. When Equal can't determin whether the args are equal or not, it returns the expression back with no changes. What you really need is SameQ here, and the code looks like this:

a = Dynamic[Floor[Clock[2]]] (*So this basically alternates between 0 and 1*)If[a === 1, Print["true"]]

-