Created
March 1, 2020 18:56
-
-
Save kritzcreek/b58a8e1bc284efb9e336e10c05015c46 to your computer and use it in GitHub Desktop.
Set Interval in PureScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- | |
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