Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  Michael E2 Sep 21 '13 at 15:30
    
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. –  m_goldberg Sep 22 '13 at 1:20
    
Thanks, m_goldberg, for showing the bug! –  Gabriel Sep 22 '13 at 7:51
add comment

1 Answer 1

up vote 2 down vote accepted

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]
}]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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