i want to run function in thread in typescript and this code before run must show loading element and run code after loading hide but not working , please help me
i use in async mode but not working , only run threadfunc and freeze screen
const RunInThread = async (threadfunc: any) => {
const loading = document.querySelector('#loading')
loading?.classList.remove('hidden')
await threadfunc()
loading?.classList.add('hidden')
}
2
Answers
You can use setTimeout to delay hiding the loading element.
Here’s how you can modify your code to achieve that:
Well, since JavaScript is single-threaded by nature, functions in a separate thread in JavaScript or TypeScript could be a little weird.
So, Basically, using Web Workers is like having a helper who can do some tasks for you in the background. While you’re doing your main stuff, the Web Worker can run separate bits of code without messing with your main work.
Well, again, you can modify your RunInThread function to use a Web Worker and Main Thread:
// worker.js
so your RunInThread function you can modify to use this worker.
Main Thread: Handle all DOM-related operations, such as showing and hiding the loading element.
Web Worker: Execute the heavy computation or processing task in the background.