Link to home
Create AccountLog in
Avatar of Rohit Bajaj
Rohit BajajFlag 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
ASKER CERTIFIED SOLUTION
Avatar of Paweł
Paweł
Flag of Switzerland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer