jaroslav@428: /** jaroslav@428: * Back 2 Browser Bytecode Translator jaroslav@428: * Copyright (C) 2012 Jaroslav Tulach jaroslav@428: * jaroslav@428: * This program is free software: you can redistribute it and/or modify jaroslav@428: * it under the terms of the GNU General Public License as published by jaroslav@428: * the Free Software Foundation, version 2 of the License. jaroslav@428: * jaroslav@428: * This program is distributed in the hope that it will be useful, jaroslav@428: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@428: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@428: * GNU General Public License for more details. jaroslav@428: * jaroslav@428: * You should have received a copy of the GNU General Public License jaroslav@428: * along with this program. Look for COPYING file in the top folder. jaroslav@428: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@428: */ jaroslav@428: package org.apidesign.bck2brwsr.htmlpage.api; jaroslav@428: jaroslav@428: import java.io.Closeable; jaroslav@428: import org.apidesign.bck2brwsr.core.JavaScriptBody; jaroslav@428: jaroslav@428: /** jaroslav@428: * jaroslav@428: * @author Jaroslav Tulach jaroslav@428: */ jaroslav@428: public class Timer implements Closeable { jaroslav@428: private final Object t; jaroslav@428: jaroslav@428: private Timer(Object t) { jaroslav@428: this.t = t; jaroslav@428: } jaroslav@428: jaroslav@428: /** Creates a timer that invokes provided runnable on a fixed interval jaroslav@428: * jaroslav@428: * @param r the runnable to execute jaroslav@428: * @param time milliseconds to invoke the timer periodically jaroslav@428: */ jaroslav@428: public static Timer create(Runnable r, int time) { jaroslav@428: return new Timer(interval(r, time)); jaroslav@428: } jaroslav@428: jaroslav@428: @JavaScriptBody(args = { "r", "time" }, body = jaroslav@428: "return window.setInterval(function() { r.run__V(); }, time);" jaroslav@428: ) jaroslav@428: private static native Object interval(Runnable r, int time); jaroslav@428: jaroslav@428: @JavaScriptBody(args = { "self" }, body = jaroslav@428: "window.clearInterval(self);" jaroslav@428: ) jaroslav@428: private static native void close(Object self); jaroslav@428: jaroslav@428: /** Cancels this timer. jaroslav@428: */ jaroslav@428: @Override jaroslav@428: public void close() { jaroslav@428: close(t); jaroslav@428: } jaroslav@428: }