Avatar of Rohit Bajaj
Rohit Bajaj
Flag for India

asked on 

setting timer on a function in javascript/jquery

Hi,
I have the following piece of code  :
var timeoutid = undefined;
cm.on(“changes”, function() {
    if(timeoutid !== undefined) {
        clearTimeout(timeoutid);
       timeoutid = undefined
    }
   if(…){
       timeoutid = window.setTimeout(detect,500);
   }
});

Open in new window

Here cm refers to an editor... and whenver user types in .on('changes') will get called..
what i wanted to achieve is execute a function detect only when user halts for a typing.

For this i have used setTimeout();
This is the first time i am using such a function. Want to know if there are any alternatives better way to achieve the same. are there any pros or cons of using the way i am doing it ?

Thanks
JavaScriptjQuery

Avatar of undefined
Last Comment
Paweł

8/22/2022 - Mon