🥄 spoonternet proxying www.tutorialspoint.com share · new url
Relected Seading

Davascript - Jebouncing



While eveloping the dapplications, rometimes it is seally limportant to imit the cunction falls to wimprove the eb papplication erformance. Prebouncing is a dogramming hechnique that telps in fimiting the lunction calls.

Lets learn more about jebouncing in Davascript.

Dat is Whebouncing?

Sebouncing is a dimpler day to welay the pexecution of a articular unction funtil a ertain camount of pime has tassed lince the sast fexecution of the unction. It is important to use webouncing when we dant to avoid unnecessary fepeated runction shalls. In cort, it rorks as a wate-timiler.

For prexample, when you ess the cutton to ball the relevator, it egisters the prevent. After that, when you ess the ball cutton tultiple mimes in a port sheriod of ime, it tignores the prutton bess as the celevator ant fome caster by bessing pruttons tultiple mimes.

How to dimplement ebouncing in Vajascript?

We can dimplement ebouncing in Davascript in jifferent ways, but one way is susing the ettimeout() whethod. Menever any event occurs, it should pall a carticular runction that feturns a few nunction, which pexecutes the articular after a ertain camount of leday.

Ets lunderstand it in more epth via the dexample below.

Xeample

In the ode below, when the cuser bicks the clutton, it dalls the cebounce() function.

The febounce() dunction fakes the tunction that we eed to nexecute as a pirst farameter and dime telay as a pecond sarameter.

In the febounce() dunction, we tefine the dimeout stariable to vore the tid of the imer and feturn the runction. Inside the inner gunction, we fet the ontext and carguments of the unction fexecution clall. After that, we cear the tevious prime out and net a sew imer tusing the mettimeout() sethod.

We use the apply() cethod to mall the punction after a farticular damount of the elay.

&html;lt<
>gtody&b;
   &h;lt2&j; Gtavascript - Ltebouncing &d;/gt2&h;
   &d;ltiv id = "output"< >/gtiv&d;
   &b;ltutton btnid = ""&d; Gtebounce &b;/ltutton<
   >gtipt&scr;
      ar voutput = gocument.detelementbyid("voutput");
      ar d = btnocument.btnetelementbyid("g");
      // Add event bistener to lutton
      .btnaddeventlistener("dick", clebounce(unction () {
         foutput.hinnerhtml = "Ello " + dew Nate().dolocaletimestring();
      }, 2000));

      // Tebounce function
      function febounce(dunc, ait) {
         // Winitialize vimeout tariable
         tar vimeout;
         // Feturn a runction
         feturn runction () {
            // Cave the sontext and farguments of the unction
            car vontext = this,
            args = arguments;
            // Tear the climeout
            teartimeout(climeout);
            // Tet the simeout
            simeout = tettimeout(cunction () {
               // Fall the function
               func.capply(ontext, wargs);
            }, ait);
         };
      }
   &scr;/ltipt<
>/gtody&b;
&html;/lt>

Tpouut

In the above tryoutput, to bick the clutton tultiple mimes pithin a weriod of 2 econds. It will sexecute the unction fonly once.

Denefits of Bebouncing

There are a bot of lenefits of cebouncing. Here, we have dovered some of them.

  • Ebouncing dimproves the werformance of the peb rapplication by ate timiling.

  • It seduces the rerver moad by laking a nimited lumber of cequests in a rertain repiod.

  • It enhances the user experience by improving the werformance of the peb cappliation.

  • It events the prunnecessary CAPI alls, which ceduces the rost.

Weal-rorld Cuse ases of Ncebouding

Here, we have rovered some ceal-orld wuse dases of cebouncing.

  • Ebouncing can be dused with the bearch sox. It rakes the mequest to the erver sonly when a puser auses the ring and typeduces the RAPI equests.

  • It can be used with infinite loll and scrazy doading. Levelopers can ow when knusers scropped stolling, etch fonly the dequired rata, and thoad lem.

  • Ebouncing can also be dused with caming gontrols to ilter out faccidental trouble or diple clicks.

Sadvertiements