1

I've looked in the pharo book and I couldn't see any examples of every:aDuration do:aBlock. I found a Timespan class which when ran does give an error when the object is created and the method is called.

|plan|
plan := Timespan new.
plan start.
" save the file every 30s"
plan
   every:30 seconds do:[ Transcript show:'My message']
erikrunia
  • 2,371
  • 1
  • 15
  • 22
ludo
  • 543
  • 3
  • 14
  • 3
    There are several examples in the tests. Open Pharo and browse the `TimespanDoTest` class. You also need to provide an argument for the `do:` block. – Leandro Caniglia Nov 27 '17 at 14:50
  • TimespanDoTest does not have a method every: do: – ludo Nov 29 '17 at 08:40
  • The class that implements `every:do` is `Timespan`. The `TimespanDoTest` class implements methods for testing `TimespanDoTest>>every:do:`. You can take a look at those tests to understand how to use the `Timespan` protocol, including `every:do:`. – Leandro Caniglia Nov 29 '17 at 13:43

2 Answers2

3

Timespan's every:do: doesn't schedule events. You might try forking a background process with a Delay to do that.

I have only Squeak handy at the moment, but it should be more or less the same.

Something like this:

planProcess := [
                 [ 30 seconds asDelay wait.
                   Transcript show: 'Saved (but not really)'; cr.
                 ] repeat.
               ] fork.

To end the process:

planProcess terminate.

There's also a Scheduler you could use for this.

fede s.
  • 582
  • 3
  • 17
  • 1
    That's exactly what I was looking for. Thanks @fede s – ludo Dec 04 '17 at 08:32
  • 2
    @ludo, no prob! I figured mostly by your comment to the other answer, so for your next Q try to be extra explicit just in case (what's obvious to you might not be to others). BTW, I don't rate this A more than "quick and dirty", take it as a point to get you started. – fede s. Dec 04 '17 at 08:40
1
|span aDate|

aDate := DateAndTime year: 2012 month: 12 day: 12.
span := Timespan starting: aDate duration: 1 minute.
span every: 10 seconds do: [ :each | Transcript show: each; cr ].

Output:

2012-12-12T00:00:00+00:00
2012-12-12T00:00:10+00:00
2012-12-12T00:00:20+00:00
2012-12-12T00:00:30+00:00
2012-12-12T00:00:40+00:00
2012-12-12T00:00:50+00:00
draegtun
  • 22,441
  • 5
  • 48
  • 71
  • I do the same but it does not repeat every 10 seconds. And how can show a message intead? – ludo Nov 29 '17 at 08:30
  • 1
    @ludo - `Timespan` doesn't *repeat every 10 seconds*. It's simply creating those `DateAndTime` objects at 10 seconds intervals for you within the time span of 2012-12-12T00:00:00 upto (but not including) 2012-12-12T00:01:00 – draegtun Dec 01 '17 at 14:20
  • 1
    I think @ludo might be looking for something like `[ 5 timesRepeat: [ 5 seconds asDelay wait. Transcript show: 'Hi!'; cr ] ] fork.` – fede s. Dec 04 '17 at 00:07
  • Yes fede thanks, I was looking for something that could repeat with delay. So Timesapn does not repeat. My mistake @draegtun thanks. – ludo Dec 04 '17 at 08:30
  • @fedes. - Yes and this was covered in an earlier SO question with ludo - https://stackoverflow.com/questions/47473641/how-can-i-refresh-a-session-for-every-60-second-and-display-it-on-transcript-wit/47486747#47486747 – draegtun Dec 04 '17 at 10:03
  • 1
    Heh, I see. Well, with the traffic in [Smalltalk] I don't think it's a problem to have new users asking simple questions ;) I'd like some more traffic in [factor-lang] too tbh... – fede s. Dec 04 '17 at 10:17
  • 1
    @fedes. - Yes it's OK for simple questions because I'm also a new user of Smalltalk and didn't even know about `Timespan` until i answered this question :) – draegtun Dec 04 '17 at 15:04