跳轉到內容

Erlang 程式設計/超時

摘自華夏公益教科書,面向開放世界的開放書籍

超時由 [ receive - after - end ] 結構建立。超時使用毫秒衡量,因此 4000 = 4 秒。我們可以使用以下程式建立一個簡單的定時器:myTimer.erl

% ============================================================ >%
-module( myTimer ).
-compile( export_all ).
                                                                %
% A simple timer that uses a timeout.
                                                                %
start( Timeout ) ->
    receive
    after Timeout ->
        io:format( "your ~w secs are up." , [Timeout/1000] )
    end.	
% ============================================================ >%
%
% Sample output:
% 
% 8> c(myTimer).
% ok.
%
% 9> myTimer:start(4000).
% your 4.00000 secs are up.ok

超時值可以是任何大於或等於 0 的數字,包括原子“infinity”。

華夏公益教科書