Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

The objective: Create a slider that, when displaced from the center, increases-decreases the value of another variable at a rate proportional to the displacement.

So, I create a slider that automatically comes back to the center

Slider[Dynamic[var, {(var = #) &, (var = 0.) &}], {-1, 1}]

Now I want a second variable to be dynamically increased/decreased when var!=0.

My ideas are:

  1. insert in the previous dynamic, something that does the updating
  2. use an animator with its speed set by var when var!=0. that updates the value

Problems with the above:

  1. How to control the speed of the updating given that I can't know for sure the intervals in which val will be updated, and UpdateIntervals sets an upper bound?
  2. How to make it work without it showing in the screen?

Any other alternatives are welcome (ideally I'd like an elegant solution).

share|improve this question
@R.M, hope this pings you. Heike's answer shows my intentions clearly. Now that Dynamic[y, {y=g[x]}] I wasn't aware of. I thought the second argument was supposed to be a function and that they only made sense when the dynamic was "being set by a control". What does it do? – Rojolalalalalalalalalalalalala Jun 3 '12 at 19:04

2 Answers 2

up vote 2 down vote accepted

Maybe you could do something like this,

var = 0.; p = 0.; interval = .2;
task = RunScheduledTask[p += var, interval];
{Slider[Dynamic[var, {(var = #) &, (var = 0.) &}], {-1, 1}], Dynamic[p]}

The downside is that rather than increasing p by a fixed amount at variable time intervals, this increases p by a variable amount at a fixed frequency. Also, you need to remember to remove task (using RemoveScheduledTask) once you're done with it.

share|improve this answer
I think this is probably a good-enough approach. +1, thanks. I'll be waiting for other options for a while to accept. One could add the run and remove scheduled tasks in the second argument of the slider's dynamic, right? So that only when dragged the task exists – Rojolalalalalalalalalalalalala Jun 3 '12 at 19:02
You could also create it once and use StartScheduledTask and StopScheduledTask in the second argument of Dynamic to start and stop it. – Heike Jun 3 '12 at 19:14
Makes sense. Create it in Initialization and remove it in Deinitialization – Rojolalalalalalalalalalalalala Jun 3 '12 at 19:15

Here's another way using Refresh that's perhaps a bit cleaner:

DynamicModule[{x = 0, y = 0},
    {Slider[Dynamic[x, {(x = #) &, (x = 0.) &}], {-1, 1}], 
     Dynamic[Refresh[y += x, TrackedSymbols :> {}, UpdateInterval -> 0.1]]}
share|improve this answer
+1. I took it and turned the second dynamic into the second argument of a DynamicWrapper so it becomes what I wanted: just a slider that does the updating. DynamicWrapper[Slider..., Refresh[...]] – Rojolalalalalalalalalalalalala Jun 3 '12 at 19:57
This is way neater, but for some reason I don't understand, Heike's solution allows me to put lower update intervals before it gets way slower than it should – Rojolalalalalalalalalalalalala Jun 3 '12 at 20:01
Interesting... didn't notice that – R. M. Jun 3 '12 at 20:12
@Rojo, R.M: This is beautiful and elegant, nice use of Refresh & DynamicWrapper! – István Zachar Jun 4 '12 at 11:12

Your Answer


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.