What is the best way of going about recreating the setTimeout() function?

Joined
Dec 14, 2021
Messages
28
Reaction score
2
I've already made a piece of code that sort of works, so here it is:

JavaScript:
function newTimingSys(functionRef, delay) {
    const timing = Date.now();
    this.func = functionRef; // the function to be called at the end of the delay
    this.count = 0;
    this.delay = delay; // must be milliseconds
    for (let i = 0; i < Math.pow(82,70); i++) {
        this.count = Date.now()-timing;
        if (this.count > this.delay) {
            this.func();
            break;
        }
    }
}

I was wondering if it is possible to make it more like setTimeout(), and if so, what would be the best method to do so? I was thinking of having it function as closely to setTimeout() as possible. Also, when I say it works, it does produce the delay, but I've only tested it on the Chrome web browser (Using a Chromebook). So here are my results with the code; when you execute the webpage the webpage will keep loading until the end of the delay. And that is what I don't want, I want it to execute just like the setTimeout().

Also, if you respond, please be very descriptive about the method, as I would love to learn. I prefer to have it be vanilla JS.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top