2
$\begingroup$

I'd like to write a function that delays the evaluation of a function (say) $5$ seconds after I hit Shift+Return. Is this possible?

$\endgroup$
2
  • $\begingroup$ The tag is wrong. I'd like to use the tag clock but it is new, and I'm not allowed to create new tags. $\endgroup$
    – zab
    Apr 28, 2014 at 15:38
  • 1
    $\begingroup$ Related: (473), (1565) $\endgroup$
    – Mr.Wizard
    Apr 28, 2014 at 19:25

2 Answers 2

6
$\begingroup$

Use the Pause function:

SetAttributes[delayedFunction, {HoldFirst}];
delayedFunction[function_] := (Pause[5]; function);
$\endgroup$
1
  • $\begingroup$ More useful if defined as delayedFunction[function_, dt_] := (Pause[dt]; function) $\endgroup$
    – m_goldberg
    Apr 28, 2014 at 19:35
3
$\begingroup$

An alternative is to use the built in function, RunScheduledTask

Dynamic[z]
RunScheduledTask[z = 7, {5}]
$\endgroup$

Your Answer

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

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