Timer.cpp 983 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/NonnullRefPtr.h>
  7. #include <LibWeb/Platform/EventLoopPlugin.h>
  8. #include <LibWeb/Platform/Timer.h>
  9. namespace Web::Platform {
  10. Timer::~Timer() = default;
  11. NonnullRefPtr<Timer> Timer::create()
  12. {
  13. return EventLoopPlugin::the().create_timer();
  14. }
  15. NonnullRefPtr<Timer> Timer::create_repeating(int interval_ms, JS::SafeFunction<void()>&& timeout_handler)
  16. {
  17. auto timer = EventLoopPlugin::the().create_timer();
  18. timer->set_single_shot(false);
  19. timer->set_interval(interval_ms);
  20. timer->on_timeout = move(timeout_handler);
  21. return timer;
  22. }
  23. NonnullRefPtr<Timer> Timer::create_single_shot(int interval_ms, JS::SafeFunction<void()>&& timeout_handler)
  24. {
  25. auto timer = EventLoopPlugin::the().create_timer();
  26. timer->set_single_shot(true);
  27. timer->set_interval(interval_ms);
  28. timer->on_timeout = move(timeout_handler);
  29. return timer;
  30. }
  31. }