如何在GHCJS程序中定期执行操作? [英] How to execute an action periodically in a GHCJS program?

查看:183
本文介绍了如何在GHCJS程序中定期执行操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该通过Javascript使用 setInterval 还是使用基于线程的一些惯用解决方案?

Should one use setInterval via Javascript, or use some more idiomatic solution based on threads?

推荐答案

使用 setInterval posed 和来自亚历山大,Erik和路易斯自己的评论让我尝试线程。这工作无缝,非常干净的代码类似于以下:

Using setInterval posed some challenges and comments from Alexander, Erik and Luite himself led me to try threads. This worked seamlessly, with very clean code similar to the following:

import Control.Concurrent( forkIO, threadDelay )
import Control.Monad( forever )

... within an IO block
threadId <- forkIO $ forever $ do
  threadDelay (60 * 1000 * 1000) -- one minute in microseconds, not milliseconds like in Javascript!
  doWhateverYouLikeHere

Haskell具有轻量级线程的概念,因此这是习惯Haskell的运行方式以异步方式执行操作,如同使用Javascript setInterval setTimeout

Haskell has the concept of lightweight threads so this is the idiomatic Haskell way to run an action in an asynchronous way as you would do with a Javascript setInterval or setTimeout.

  • Hackage
  • Real world Haskell

这篇关于如何在GHCJS程序中定期执行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆