3
$\begingroup$

I'm basing this on the code found in the Introduction to Dynamics. I added one more slider which doesn't use the "round and snap" feature given in the link above:

DynamicModule[{x = 0},
 Column@{Slider[
    Dynamic[x, 
     If[Abs[# - Round[#]] < 0.3, x = Round[x], x = #] &], {0, 5}]
   , Slider[Dynamic[x], {0, 5}]
   , Dynamic[x]
   }]

When sliding the second slider, I would have though the first one one stay snapped until x > 0.3, but that is not the case: the first slider moves together with the second one.

Is the update function only evaluating when the first slider is moved?

$\endgroup$
3
  • $\begingroup$ Yes. Each slider has its own update function that is applied only when that slider is moved. However the positions of both sliders always track the value of x. $\endgroup$
    – Michael E2
    Commented Sep 21, 2013 at 15:30
  • $\begingroup$ There is a bug in your posted code. The constraint function should be If[Abs[# - Round[#]] < 0.3, x = Round[#], x = #] &. Note the additional # in the 2nd arg to If. $\endgroup$
    – m_goldberg
    Commented Sep 22, 2013 at 1:20
  • $\begingroup$ Thanks, m_goldberg, for showing the bug! $\endgroup$
    – Gabriel
    Commented Sep 22, 2013 at 7:51

1 Answer 1

2
$\begingroup$

You've only set the update function to snap in the first, so it will always show the current value of x and only when you dynamically change it using that slider, will the updating functionality be trickers causing it to snap. You can simply get what you are expecting by also including the snap functionality in the dynamically displayed value through:

DynamicModule[{x = 0},
 Column@{Slider[
  Dynamic[If[Abs[x- Round[x]] < 0.3, Round[x], x], 
      If[Abs[# - Round[#]] < 0.3, x = Round[x], x = #] &], {0, 5}]
, Slider[Dynamic[x], {0, 5}]
, Dynamic[x]
}]
$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.