Skip to content

How to implement setInterval? #11

Open
@adhamsalama

Description

@adhamsalama

Hello.

I had so much fun following this tutorial.

I implemented setTimeout using the answer to my question here denoland/deno#19231

#[op]
async fn op_set_timeout(delay: u64) -> Result<(), AnyError> {
    tokio::time::sleep(tokio::time::Duration::from_millis(delay)).await;
    Ok(())
}

This works fine because it does nothing but wait and then return to JavaScript where we can execute the callback.

But how could we implement setInterval?
We need to execute the JavaScript callback after every delay, and also return a timer ID so that JavaScript could cancel it later. I don't get how to do all of that in a single function.

So, could you please help me? Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions