Skip to content

Instantly share code, notes, and snippets.

@kritzcreek
Created March 1, 2020 18:56
Show Gist options
  • Save kritzcreek/b58a8e1bc284efb9e336e10c05015c46 to your computer and use it in GitHub Desktop.
Save kritzcreek/b58a8e1bc284efb9e336e10c05015c46 to your computer and use it in GitHub Desktop.
Set Interval in PureScript
module Main where
import Prelude
import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect.Console (log)
import Effect.Random as Random
import Effect.Ref as Ref
import Effect.Timer (clearInterval, setInterval)
main :: Effect Unit
main = do
idRef <- Ref.new Nothing
id <- setInterval 50 do
log "Hello from setInterval"
randomNumber <- Random.random
if randomNumber < 0.2 then
Ref.read idRef >>= case _ of
Nothing -> pure unit
Just id -> clearInterval id
else
pure unit
Ref.write (Just id) idRef
{-
Welcome to a Spago project!
You can edit this file as you like.
-}
{ name = "set-interval"
, dependencies =
[ "console"
, "effect"
, "js-timers"
, "maybe"
, "psci-support"
, "random"
, "refs"
]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment