A
A
Arbil2019-10-07 15:02:14
Pug
Arbil, 2019-10-07 15:02:14

Timer and linearity?

Hello
I'm new to your field
And I can't figure out how a timer works and something else at the same time
If programming languages ​​are done like this:
---> a = 6
---> if a == 6:
---> ..
So how can two subroutines run at the same time ? ( my function and timer )
---> myFunc() and then timer()
PS please don't laugh if the question seemed silly to you

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Matvey Phoenix, 2018-06-28
@TSergey93

I have such an example - the template is one, but the title is different. How I did it:
1) Template file:

doctype html

block variables

html(lang= "ru")
  head
    meta(charset="utf-8")
    title #{title}

2) File directly page:
extends ../_template

block variables
  - var title = "Главная страница"

block content

Everything works, you can see this project , there are a lot of variables there, for highlighting menu items, for substituting a banner on only one page.
PS do not forget to write the correct path to the template, here - extends ../_template

T
tsarevfs, 2019-10-07
@Arbil

There may be different options here. But the most common is "event-driven programming".
In simplified form, it looks like this:

while (true) //event loop
{
  if (queue.empty())
    continue;

  auto event = queue.front();
  queue.pop_front();

  process(event);
}
onButton()
{
  ...
}
onTimer()
{
  ...
}
process(Event event)
{
  if (isTimetEvent(event))
    onTimer();

  if (isButtonEvent(event))
    onButton();
}

Most of the time the program spins in an eternal loop. But when events occur (events), they are added to the queue (queue) and their handlers are called. The expiration of the timer also becomes an event.
The trick is that the handlers of such events cannot work for a long time, otherwise the rest will be blocked. For example, it's a bad idea to wait in a user input handler or to do large calculations. Streams and other techniques are used to solve these problems.
In the code, I did not describe adding events to the queue. This can happen in many ways. But in the simplest case, at each iteration, we can poll the button timers and other sources about the events that have occurred.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question