summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/HTML/Timer.cpp
blob: 1ea86148f692a06f8ba25436aa8e214803cab3af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibCore/Timer.h>
#include <LibWeb/HTML/Timer.h>
#include <LibWeb/HTML/Window.h>

namespace Web::HTML {

NonnullRefPtr<Timer> Timer::create(Window& window, i32 milliseconds, Function<void()> callback, i32 id)
{
    return adopt_ref(*new Timer(window, milliseconds, move(callback), id));
}

Timer::Timer(Window& window, i32 milliseconds, Function<void()> callback, i32 id)
    : m_window(window)
    , m_id(id)
{
    m_timer = Core::Timer::create_single_shot(milliseconds, [this, callback = move(callback)] {
        NonnullRefPtr strong_timer { *this };
        callback();
    });
}

Timer::~Timer()
{
    m_window.deallocate_timer_id({}, m_id);
}

void Timer::start()
{
    m_timer->start();
}

}