Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

window.setInterval and window.setTimeout have a [timeout] argument which does not work #339

Open
ShubhamVG opened this issue Feb 9, 2025 · 3 comments
Assignees

Comments

@ShubhamVG
Copy link

The function declaration of both window.setInterval and window.setTimeout look the same i.e.,

  external int setInterval(
    TimerHandler handler,
    JSAny? arguments, [
    int timeout,
  ]);

And according to MDN, the JS declaration is

setInterval(func, delay, arg1, arg2, /* …, */ argN)

At first, I thought that timeout is the delay parameter but it wasn't. Setting timeout did not affect the delay at all.
After that, I thought that maybe timeout is the time after which the interval closes/clears itself. That does not turn out to be the case either.

So far, setting timeout seems to be NOT do anything and I could not find any JS equivalent feature either.

So, that's why I have this question: What does the [timeout] argument do?

@ykmnkmi
Copy link

ykmnkmi commented Feb 10, 2025

I think the timeout should be the second argument, and the rest should be the other arguments.

import 'dart:js_interop';

import 'package:web/web.dart';

void main() {
  JSExportedDartFunction jsHandler = onEvent.toJS;
  window.setInterval(jsHandler, /* timeout */ 1000.toJS, /* args */ 1);
}

void onEvent(/* args */ int value) {
  log(value.toJS); // 1
}

@JS('console.log')
external void log(JSAny message);

@ykmnkmi
Copy link

ykmnkmi commented Feb 10, 2025

Use this (remove args if you don't need them)

extension on Window {
  @JS('setInterval')
  external int setInterval$(
    TimerHandler handler,
    int timeout, [
    JSAny? args,
  ]);
}

@srujzs
Copy link
Contributor

srujzs commented Feb 10, 2025

https://github.com/w3c/webref/blob/6e9c0167c76cd5aaaf01b51669639bb7e916d52b/ed/idl/html.idl#L2300

It looks like we're not treating variadic arguments as optional. Because we believe optional parameters should always go after required ones, the ordering gets flipped:

return _overridableMember<code.Method>(
.

I believe the fix should just be to consider variadic arguments as optional. It looks like that is the intention of the spec:

An argument is considered to be an optional argument if it is declared with the optional keyword. The final argument of a variadic operation is also considered to be an optional argument.

Olzhas is correct here wrt a workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants