How do you handle multiple instances of setTimeout()?

前端 未结 9 2047
鱼传尺愫
鱼传尺愫 2020-12-01 05:18

What is the most recommended/best way to stop multiple instances of a setTimeout function from being created (in javascript)?

An example (psuedo code):



        
9条回答
  •  自闭症患者
    2020-12-01 06:01

    I'm using this to force a garbage collection on all obsolete timeout references which really un-lagged my script preformance:

    var TopObjList = new Array();
    function ColorCycle( theId, theIndex, RefPoint ) {
        ...
        ...
        ...
        TopObjList.push(setTimeout( function() { ColorCycle( theId, theIndex ,CCr ); },CC_speed));
        TO_l = TopObjList.length;
        if (TO_l > 8888) {
            for (CCl=4777; CCl

    My original sloppy code was generating a massive array 100,000+ deep over a very short time but this really did the trick!

提交回复
热议问题