How to stop the timer by clicking the button twice in GUI, AND how can I return the number of the time I pause?

2 vues (au cours des 30 derniers jours)
I write a countdown timer and want to stop it by click it twice and return the number on the screen when I stop it?
function [] = timer_examp()
S.fh = figure('units','pixels',...
'position',[500 500 200 100],...
'menubar','none',...
'name','Timer_examp',...
'numbertitle','off',...
'resize','off');
S.tx = uicontrol('style','text',...
'unit','pix',...
'position',[10 60 180 30],...
'fontsize',14,...
'string','60'); % the beggining time was in 60s
S.tmr = timer('Name','Countdown',...
'Period',1,... % 1s update period.
'StartDelay',.01,... % update delay period is 0.01s
'ExecutionMode','fixedSpacing',...
'StopFcn',@deleter); % Function def. below.
S.pb = uicontrol('style','push',...
'units','pix',...
'position',[10 10 180 40],...
'fontsize',14,...
'string','Start Countdown!',...
'callback',{@pb_call,S});
set(S.tmr,'TimerFcn',{@update_disp,S},'StartDelay',1);
function [] = pb_call(varargin)
% Callback and delete from list box
S = varargin{3}
% word string checking
set(S.tmr,'TasksToExecute',str2double(get(S.tx,'string')))
start(S.tmr);
function [] = update_disp(varargin)
S = varargin{3};
N = str2double(get(S.tx,'string'));
set(S.tx,'string',num2str(N-1));
function deleter(obj,edata) %#ok M-Lint doesn't know the callback fmt.
% Callback stopfcn.
wait(obj);
delete(obj);

Réponses (1)

Geoff Hayes
Geoff Hayes le 21 Déc 2022
@Dongze - is there a reason why you want to stop the timer via a double-click of a button that says "Start Countdown"? Perhaps you can use a different button to pause/stop the timer. If though you really want to do this with a double-click, you could try the following
function [] = pb_call(varargin)
% not keen on using a persistent/static but...
persistent firstButtonPressTime
S = varargin{3};
if (strcmpi(S.tmr.Running,'on'))
% if timer is running, then save the current time or compare against
% the last saved time
if isempty(firstButtonPressTime)
firstButtonPressTime = now;
else
currentTime = now;
% determine the delta in seconds
xDeltaInSeconds = (currentTime - firstButtonPressTime) * 24 * 60 * 60;
% stop timer if two consecutive button presses are less than 0.5
% seconds apart
if xDeltaInSeconds < 0.5
firstButtonPressTime = [];
stop(S.tmr);
else
firstButtonPressTime = currentTime;
end
end
else
set(S.tmr,'TasksToExecute',str2double(get(S.tx,'string')))
start(S.tmr);
end

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by