IntroductionInstallationLimitations of the "file" protocolFrequently asked questionsSyntax, keywords and built-in functionsStandard distributionimport implementationBrython packagesBrowser interface
Introduction - DOM API
Creating a document Accessing elements Attributes, properties and methods Events Mouse events Keyboard events Focus events Drag events Query string Interactions with Javascript Brython-specific built-in modules
browser
browser.aio browser.ajax browser.html browser.local_storage browser.markdown browser.object_storage browser.session_storage browser.svg browser.template browser.timer browser.webcomponent browser.websocket browser.worker Widgets browser.widgets.dialog browser.widgets.menu interpreter javascript Working with BrythonCookbook |
module browser.timerImplements methods to allow differed or repetitive execution of functions :set_timeout(function, ms, *args)
runs
It is a wrapper of the
In this simple example, the color of the text in the black box will change after 3 seconds. from browser import document as doc
from browser import timer
def change_color():
doc['first-text'].style.color = "blue"
def press_button(ev):
timer.set_timeout(change_color, 3000)
doc['first-button'].bind('click', press_button)
clear_timeout(timer)
cancels the execution of the function defined by
It is a wrapper of the
Let's see the previous example. Now you have the possibility to stop the execution of the action before the 3 seconds that delays in the execution. from browser import document, timer
idtimer = 1
def change_color_two():
document['ct-text2'].style.color = "blue"
def press_button_two(ev):
global idtimer
idtimer = timer.set_timeout(change_color_two, 3000)
def stop_button(ev):
timer.clear_timeout(idtimer)
document['ct-start'].bind('click', press_button_two)
document['ct-stop'].bind('click', stop_button)
set_interval(function, ms, *args)
launches repeated execution of
It is a wrapper of the
When possible, you should avoid the use of this function and use request_animation_frame (see below) as an alternative. clear_interval(timer)
stops the repeated execution of the function defined by
It is a wrapper of the
Here you could see an example where it is used import time
from browser import document as doc
from browser import timer
_timer = None
counter = 0
def show():
doc['_timer'].text = '%.2f' %(time.time()-counter)
def start_timer(ev):
global _timer,counter
if _timer is None:
counter = time.time()
_timer = timer.set_interval(show,10)
doc['start'].text = 'Hold'
elif _timer == 'hold': # restart
# restart timer
counter = time.time()-float(doc['_timer'].text)
_timer = timer.set_interval(show,10)
doc['start'].text = 'Hold'
else: # hold
timer.clear_interval(_timer)
_timer = 'hold'
doc['start'].text = 'Restart'
def stop_timer(ev):
global _timer
timer.clear_interval(_timer)
_timer = None
t = 0
doc['_timer'].text = '%.2f' %0
doc['start'].text = 'Start'
doc['start'].bind('click', start_timer)
doc['stop'].bind('click', stop_timer)
request_animation_frame(*function*)
runs the function repeatedly letting the browser be in charge to update the browser. function uses a fake argument
It is a wrapper of the
cancel_animation_frame(*id*)
cancels the repeated execution of the function defined by request_animation_frame() and uses the value returned by request_animation_frame() as id
It is a wrapper of the
Here you could see an example where it is used from browser.timer import request_animation_frame as raf
from browser.timer import cancel_animation_frame as caf
from browser import document as doc
from browser import window as win
from time import time
from browser.html import CANVAS, BUTTON
import math
ctx = doc['raf-canvas'].getContext( '2d' )
toggle = True
def draw():
t = time() * 3
x = math.sin(t) * 96 + 128
y = math.cos(t * 0.9) * 96 + 128
global toggle
if toggle:
toggle = False
else:
toggle = True
ctx.fillStyle = 'rgb(200,200,20)' if toggle else 'rgb(20,20,200)'
ctx.beginPath()
ctx.arc( x, y, 6, 0, math.pi * 2, True)
ctx.closePath()
ctx.fill()
def animate(i):
global id
id = raf(animate)
draw()
def stop(i):
global id
print(id)
caf(id)
doc['btn-animate'].bind('click', animate)
doc['btn-stop'].bind('click', stop)
|