How to use setTimeout in PureScript v0.7

I want to use setTimeout to animate in PureScript like this.

loop n =
  if n > 100
  then do
    return Unit
  else do
    print n
    timeout (loop n+1) 30

      

purescript-timers no longer works in v0.7.

I have no idea how to implement this.

+3


source to share


2 answers


There are two ways:



I prefer later and here's an example:

import Control.Monad.Aff as Aff

update :: forall eff. Action -> State -> EffModel State Action (eff)
update MyAction myState = 
  { state: myState, effects: [ Aff.later' 1000 $ pure MyOtherAction ] }

      

+1


source


The easiest way is to define your own imports for setTimeout

:

module SetTimeout where

foreign import data TIMEOUT :: !

foreign import timeout :: forall eff a. 
                               Int -> 
                               Eff (timeout :: TIMEOUT | eff) a -> 
                               Eff (timeout :: TIMEOUT | eff) Unit

      

In your external Javascript module, you can define setTimeout

like this:

"use strict";

// module SetTimeout

exports.timeout = function(millis) {
    return function(action) {
        return function() {
            setTimeout(action, millis);
        };
    };
};

      



You could extend this to work with type things clearTimeout

if needed.

Some other possible approaches:

  • Submit a pull request for an upgrade purescript-timers

    by following the migration guide on the wiki: https://github.com/purescript/purescript/wiki/0.7-Migration-Guide
  • Use later'

    from purescript-aff

    : doc
+2


source







All Articles