- 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:
I was wondering if it is possible to make it more like
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.
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: