From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Johan Andersson Newsgroups: gmane.emacs.help Subject: Timer variable binding Date: Tue, 7 Jan 2014 15:53:25 +0100 Message-ID: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: ger.gmane.org 1389106434 16335 80.91.229.3 (7 Jan 2014 14:53:54 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 7 Jan 2014 14:53:54 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Jan 07 15:54:01 2014 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1W0Y2a-0005mw-Ap for geh-help-gnu-emacs@m.gmane.org; Tue, 07 Jan 2014 15:54:00 +0100 Original-Received: from localhost ([::1]:41111 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W0Y2a-0004Kc-10 for geh-help-gnu-emacs@m.gmane.org; Tue, 07 Jan 2014 09:54:00 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:35375) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W0Y2O-0004KP-Ce for help-gnu-emacs@gnu.org; Tue, 07 Jan 2014 09:53:49 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W0Y2N-00038l-1q for help-gnu-emacs@gnu.org; Tue, 07 Jan 2014 09:53:48 -0500 Original-Received: from mail-oa0-x22d.google.com ([2607:f8b0:4003:c02::22d]:43369) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W0Y2M-00038e-T2 for help-gnu-emacs@gnu.org; Tue, 07 Jan 2014 09:53:46 -0500 Original-Received: by mail-oa0-f45.google.com with SMTP id g12so268560oah.32 for ; Tue, 07 Jan 2014 06:53:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=yhrXA9CPPGMFvirTpqsa22cUu2BsA/GJeexyYwNBcKg=; b=UXl06TsCZoeC3jF9DWH5w5fFDO2vtWcWwWJElWPd/Eq804X64s0oPMt70LjMAJU8Cm jzcw2q4w8yq4npvk2/qFu26YIDKxAWCo8HWWvO5kiMORd0dBAjXBYrtdWP8a3kenLKgk 66pOMubdF2qMQEDOvWFCSeAoG6lj+Yl/U45Vmx9JnQ4voybWFmo1C25uUfnDI+C8+DZq fIGw4ldNBDpqpeLKrD/1RRlSqJFnMlkzFKuDsLAaTSa8e08eUD2ezHOc34GCJFJrCP1G udriWXCsS/Id8uRz+O3qaDQUsClXhEMwt55IHvZEEQbA0wU3Z8y4vsPj/MwMGwnnPlQN 5K5g== X-Received: by 10.60.35.73 with SMTP id f9mr1922116oej.50.1389106425846; Tue, 07 Jan 2014 06:53:45 -0800 (PST) Original-Received: by 10.182.154.73 with HTTP; Tue, 7 Jan 2014 06:53:25 -0800 (PST) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4003:c02::22d X-Content-Filtered-By: Mailman/MimeDel 2.1.14 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:95294 Archived-At: Hi, I have some questions regarding timers. This code will start a timer and the first time the callback runs, the timer is canceled. Works great! (let ((timer (run-at-time 0 1 (lambda () (cancel-timer timer)))))) In the above example I can access the timer variable inside the function callback. But in this code, I cannot access the variable my-var. Nothing is printed. In Emacs 24.3.1 I see no error, but in 24.3.50.1 I get (void-variable my-var). Why is there no error in 24.3.1? (let* (timer (my-var 10)) (setq timer (run-at-time 0 1 (lambda () (print my-var) (cancel-timer timer))))) I can solve this by passing the my-var variable as argument to run-at-time, but then that same value will be passed to the function callback each time. Let's say for example that I want to run a timer 10 times, I can't do this using the code below, because my-var will always have value 10: (let* (timer (my-var 10)) (setq timer (run-at-time 0 1 (lambda (my-var) (setq my-var (1- my-var)) (when (= my-var 0) (cancel-timer timer))) my-var))) Something else worth noting is that in the callback function, it is possible to access globally defined variables. Can someone please explain these weird behaviors?