Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
Can setTimeout on KeyDown affect performance?

With the help of a fellow Stack Overflow member I got a code for having shortcuts using Keydown.

How to Check if jQuery .keydown() has any other keys pressed or not?

The code uses setTimeout function for every single keydown event. I am using it on a page where users usually type more than 1000 words. (4k-5k characters I assume)

This means Keydown would run on all those 5k keydowns and new timeouts will starts many times.


    var NewKeys = {};
    var NewTimeout;
    jQuery(document).keydown(function (e) {
        if (NewTimeout) {
            clearTimeout(NewTimeout);
        }
        NewKeys[e.which] = true;
        NewTimeout  = setTimeout(function () {
            if (typeof NewKeys[17] !== "undefined"  && typeof NewKeys[83] !== "undefined"  && Object.keys(NewKeys).length === 2) { 
                e.preventDefault();
                if(!jQuery('.SearchCreations').hasClass('Show')){
                    e.preventDefault();
                    jQuery('.SaveCreation').click();
                }
            }
            if (typeof NewKeys[27] !== "undefined" && Object.keys(NewKeys).length === 1) { 
                jQuery('.Thumbnail.Close').click();
                jQuery('.Action').removeClass('Collapse');
            }
            if (typeof NewKeys[16] !== "undefined" && typeof NewKeys[17] !== "undefined"  && typeof NewKeys[83] !== "undefined"  && Object.keys(NewKeys).length === 3) {
                e.preventDefault();
                if(jQuery('.CreationEditor').hasClass('Autosaving')){
                    jQuery('.StopSave').click();
                }
                else{
                    jQuery('.AutoSave').click();
                }
            }
          }, 200);
    });

Can this affect performance/reliability for the end user.

The last thing you want to do is have a webpage that is so resource intensive that your user's device stops working properly.

I tried this code on 6-7 year old laptop (i5, 4GB RAM, Windows 10). I noticed that if I keep using the page it works fine. But if I switch window to some other application using Alt + Tab and return back to browser in about 5-7 sec to my site, the Shortcut keys would stop working.

Can confirm this behavior is consistent, and it is reproducable on multiple Windows 10 machines as of now.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This pretty much depends on your timeout value and also the speed of your typing. For example your timeout is 3 and you type 5 letters in second. Then you will end up with 15 functions that run simultaneously.

It's fine, but when you use a longer timeout, like maybe 10s and your speed is 10 letters/sec, then you will have 100 functions running at the same time which is not appropriate.

Specially if you try to do heavier tasks than changing classes and some DOM manipulations.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!