2011-09-30 3 views
4

내가 제안 된 몇 가지 질문을 읽고에 화재하지만이를 구현하는 방법을 정확히 모르겠습니다 전에 지연을 추가 :의 keyup()는 jQuery를

나는 8 개의 텍스트 박스를 가지고 있으며, 7 개는 숫자 입력에 사용되며, 8 번째는 총합이다. 즉, 7.5 + 7.5 + 7.5 + 7.5 + 7.5 + 0.0 + 0.0 = 37.5

각 텍스트 상자 keyup()을 모니터링하고 모든 값을 추가하고 합계를 계산하도록 jQuery가 작동합니다.

필자는 7 개의 텍스트 상자에 입력 된 사용자의 서식을 "1"또는 ".1"즉 "1.0"또는 "0.1"을 입력하면 #. #처럼 표시되도록 결정했습니다.

문제는 입력이 포맷되기 전에 지연을 추가해야하며 자바 스크립트 및/또는 jquery로 어떻게 수행하는지 확신 할 수 없다는 것입니다. 예를 들어, 지정된 시간 후

$('input[id*="txtMondayHours"]').val((new Number($('input[id*="txtMondayHours"]').val())).toFixed(2)); 

:

<script type="text/javascript"> 

     $(function() { 

      var content = $('input[id*="txtMondayHours"]').val(); 
      $('input[id*="txtMondayHours"]').keyup(function() { 
       if ($('input[id*="txtMondayHours"]').val() != content) { 

        content = $('input[id*="txtMondayHours"]').val(); 
        $('input[id*="txtMondayHours"]').val((new Number($('input[id*="txtMondayHours"]').val())).toFixed(2)); 
        var hoursMon = new Number(content); 
        var hoursTue = new Number($('input[id*="txtTuesdayHours"]').val()); 
        var hoursWed = new Number($('input[id*="txtWednesdayHours"]').val()); 
        var hoursThu = new Number($('input[id*="txtThursdayHours"]').val()); 
        var hoursFri = new Number($('input[id*="txtFridayHours"]').val()); 
        var hoursSat = new Number($('input[id*="txtSaturdayHours"]').val()); 
        var hoursSun = new Number($('input[id*="txtSundayHours"]').val()); 

        $('input[id*="txtTotalWorkingHours"]').val(parseFloat(hoursMon.toFixed(2)) + parseFloat(hoursTue.toFixed(2)) + parseFloat(hoursWed.toFixed(2)) + parseFloat(hoursThu.toFixed(2)) + parseFloat(hoursFri.toFixed(2)) + parseFloat(hoursSat.toFixed(2)) + parseFloat(hoursSun.toFixed(2))); 
       } 
      }); 

      var content2 = $('input[id*="txtTuesdayHours"]').val(); 
      $('input[id*="txtTuesdayHours"]').keyup(function() { 
       if ($('input[id*="txtTuesdayHours"]').val() != content2) { 

        content2 = $('input[id*="txtTuesdayHours"]').val(); 
        var hoursMon = new Number(content2); 
        var hoursTue = new Number($('input[id*="txtMondayHours"]').val()); 
        var hoursWed = new Number($('input[id*="txtWednesdayHours"]').val()); 
        var hoursThu = new Number($('input[id*="txtThursdayHours"]').val()); 
        var hoursFri = new Number($('input[id*="txtFridayHours"]').val()); 
        var hoursSat = new Number($('input[id*="txtSaturdayHours"]').val()); 
        var hoursSun = new Number($('input[id*="txtSundayHours"]').val()); 

        $('input[id*="txtTotalWorkingHours"]').val(parseFloat(hoursMon.toFixed(2)) + parseFloat(hoursTue.toFixed(2)) + parseFloat(hoursWed.toFixed(2)) + parseFloat(hoursThu.toFixed(2)) + parseFloat(hoursFri.toFixed(2)) + parseFloat(hoursSat.toFixed(2)) + parseFloat(hoursSun.toFixed(2))); 
       } 
      }); 

      var content3 = $('input[id*="txtWednesdayHours"]').val(); 
      $('input[id*="txtWednesdayHours"]').keyup(function() { 
       if ($('input[id*="txtWednesdayHours"]').val() != content3) { 

        content3 = $('input[id*="txtWednesdayHours"]').val(); 
        var hoursMon = new Number(content3); 
        var hoursTue = new Number($('input[id*="txtMondayHours"]').val()); 
        var hoursWed = new Number($('input[id*="txtTuesdayHours"]').val()); 
        var hoursThu = new Number($('input[id*="txtThursdayHours"]').val()); 
        var hoursFri = new Number($('input[id*="txtFridayHours"]').val()); 
        var hoursSat = new Number($('input[id*="txtSaturdayHours"]').val()); 
        var hoursSun = new Number($('input[id*="txtSundayHours"]').val()); 

        $('input[id*="txtTotalWorkingHours"]').val(parseFloat(hoursMon.toFixed(2)) + parseFloat(hoursTue.toFixed(2)) + parseFloat(hoursWed.toFixed(2)) + parseFloat(hoursThu.toFixed(2)) + parseFloat(hoursFri.toFixed(2)) + parseFloat(hoursSat.toFixed(2)) + parseFloat(hoursSun.toFixed(2))); 
       } 
      }); 

      var content4 = $('input[id*="txtThursdayHours"]').val(); 
      $('input[id*="txtThursdayHours"]').keyup(function() { 
       if ($('input[id*="txtThursdayHours"]').val() != content4) { 

        content4 = $('input[id*="txtThursdayHours"]').val(); 
        var hoursMon = new Number(content4); 
        var hoursTue = new Number($('input[id*="txtMondayHours"]').val()); 
        var hoursWed = new Number($('input[id*="txtTuesdayHours"]').val()); 
        var hoursThu = new Number($('input[id*="txtWednesdayHours"]').val()); 
        var hoursFri = new Number($('input[id*="txtFridayHours"]').val()); 
        var hoursSat = new Number($('input[id*="txtSaturdayHours"]').val()); 
        var hoursSun = new Number($('input[id*="txtSundayHours"]').val()); 

        $('input[id*="txtTotalWorkingHours"]').val(parseFloat(hoursMon.toFixed(2)) + parseFloat(hoursTue.toFixed(2)) + parseFloat(hoursWed.toFixed(2)) + parseFloat(hoursThu.toFixed(2)) + parseFloat(hoursFri.toFixed(2)) + parseFloat(hoursSat.toFixed(2)) + parseFloat(hoursSun.toFixed(2))); 
       } 
      }); 

      var content5 = $('input[id*="txtFridayHours"]').val(); 
      $('input[id*="txtFridayHours"]').keyup(function() { 
       if ($('input[id*="txtFridayHours"]').val() != content5) { 

        content5 = $('input[id*="txtFridayHours"]').val(); 
        var hoursMon = new Number(content5); 
        var hoursTue = new Number($('input[id*="txtMondayHours"]').val()); 
        var hoursWed = new Number($('input[id*="txtWednesdayHours"]').val()); 
        var hoursThu = new Number($('input[id*="txtThursdayHours"]').val()); 
        var hoursFri = new Number($('input[id*="txtTuesdayHours"]').val()); 
        var hoursSat = new Number($('input[id*="txtSaturdayHours"]').val()); 
        var hoursSun = new Number($('input[id*="txtSundayHours"]').val()); 

        $('input[id*="txtTotalWorkingHours"]').val(parseFloat(hoursMon.toFixed(2)) + parseFloat(hoursTue.toFixed(2)) + parseFloat(hoursWed.toFixed(2)) + parseFloat(hoursThu.toFixed(2)) + parseFloat(hoursFri.toFixed(2)) + parseFloat(hoursSat.toFixed(2)) + parseFloat(hoursSun.toFixed(2))); 
       } 
      }); 

      var content6 = $('input[id*="txtSaturdayHours"]').val(); 
      $('input[id*="txtSaturdayHours"]').keyup(function() { 
       if ($('input[id*="txtSaturdayHours"]').val() != content6) { 

        content6 = $('input[id*="txtSaturdayHours"]').val(); 
        var hoursMon = new Number(content6); 
        var hoursTue = new Number($('input[id*="txtMondayHours"]').val()); 
        var hoursWed = new Number($('input[id*="txtWednesdayHours"]').val()); 
        var hoursThu = new Number($('input[id*="txtThursdayHours"]').val()); 
        var hoursFri = new Number($('input[id*="txtFridayHours"]').val()); 
        var hoursSat = new Number($('input[id*="txtTuesdayHours"]').val()); 
        var hoursSun = new Number($('input[id*="txtSundayHours"]').val()); 

        $('input[id*="txtTotalWorkingHours"]').val(parseFloat(hoursMon.toFixed(2)) + parseFloat(hoursTue.toFixed(2)) + parseFloat(hoursWed.toFixed(2)) + parseFloat(hoursThu.toFixed(2)) + parseFloat(hoursFri.toFixed(2)) + parseFloat(hoursSat.toFixed(2)) + parseFloat(hoursSun.toFixed(2))); 
       } 
      }); 

      var content7 = $('input[id*="txtSundayHours"]').val(); 
      $('input[id*="txtSundayHours"]').keyup(function() { 
       if ($('input[id*="txtSundayHours"]').val() != content7) { 

        content7 = $('input[id*="txtSundayHours"]').val(); 
        var hoursMon = new Number(content7); 
        var hoursTue = new Number($('input[id*="txtMondayHours"]').val()); 
        var hoursWed = new Number($('input[id*="txtWednesdayHours"]').val()); 
        var hoursThu = new Number($('input[id*="txtThursdayHours"]').val()); 
        var hoursFri = new Number($('input[id*="txtFridayHours"]').val()); 
        var hoursSat = new Number($('input[id*="txtSaturdayHours"]').val()); 
        var hoursSun = new Number($('input[id*="txtTuesdayHours"]').val()); 

        $('input[id*="txtTotalWorkingHours"]').val(parseFloat(hoursMon.toFixed(2)) + parseFloat(hoursTue.toFixed(2)) + parseFloat(hoursWed.toFixed(2)) + parseFloat(hoursThu.toFixed(2)) + parseFloat(hoursFri.toFixed(2)) + parseFloat(hoursSat.toFixed(2)) + parseFloat(hoursSun.toFixed(2))); 
       } 
      }); 

     }); 
    </script> 

이상적으로,이 선을 발사하고 싶습니다 100ms의


업데이트 작업 코드 :


<script type="text/javascript"> 

     var delay = (function() { 
      var timer = 0; 
      return function (callback, ms) { 
       clearTimeout(timer); 
       timer = setTimeout(callback, ms); 
      }; 
     })(); 

     $(function() { 

      var content = $('input[id*="txtMondayHours"]').val(); 
      $('input[id*="txtMondayHours"]').keyup(function() { 
       if ($('input[id*="txtMondayHours"]').val() != content) { 

        content = $('input[id*="txtMondayHours"]').val(); 
        var hoursMon = new Number(content); 
        var hoursTue = new Number($('input[id*="txtTuesdayHours"]').val()); 
        var hoursWed = new Number($('input[id*="txtWednesdayHours"]').val()); 
        var hoursThu = new Number($('input[id*="txtThursdayHours"]').val()); 
        var hoursFri = new Number($('input[id*="txtFridayHours"]').val()); 
        var hoursSat = new Number($('input[id*="txtSaturdayHours"]').val()); 
        var hoursSun = new Number($('input[id*="txtSundayHours"]').val()); 

        $('input[id*="txtTotalWorkingHours"]').val(parseFloat(hoursMon.toFixed(2)) + parseFloat(hoursTue.toFixed(2)) + parseFloat(hoursWed.toFixed(2)) + parseFloat(hoursThu.toFixed(2)) + parseFloat(hoursFri.toFixed(2)) + parseFloat(hoursSat.toFixed(2)) + parseFloat(hoursSun.toFixed(2))); 

        delay(function() { 
         $('input[id*="txtMondayHours"]').val((new Number($('input[id*="txtMondayHours"]').val())).toFixed(2)); 
        }, 750); 
       } 
      }); 

      var content2 = $('input[id*="txtTuesdayHours"]').val(); 
      $('input[id*="txtTuesdayHours"]').keyup(function() { 
       if ($('input[id*="txtTuesdayHours"]').val() != content2) { 

        content2 = $('input[id*="txtTuesdayHours"]').val(); 
        var hoursMon = new Number(content2); 
        var hoursTue = new Number($('input[id*="txtMondayHours"]').val()); 
        var hoursWed = new Number($('input[id*="txtWednesdayHours"]').val()); 
        var hoursThu = new Number($('input[id*="txtThursdayHours"]').val()); 
        var hoursFri = new Number($('input[id*="txtFridayHours"]').val()); 
        var hoursSat = new Number($('input[id*="txtSaturdayHours"]').val()); 
        var hoursSun = new Number($('input[id*="txtSundayHours"]').val()); 

        $('input[id*="txtTotalWorkingHours"]').val(parseFloat(hoursMon.toFixed(2)) + parseFloat(hoursTue.toFixed(2)) + parseFloat(hoursWed.toFixed(2)) + parseFloat(hoursThu.toFixed(2)) + parseFloat(hoursFri.toFixed(2)) + parseFloat(hoursSat.toFixed(2)) + parseFloat(hoursSun.toFixed(2))); 

        delay(function() { 
         $('input[id*="txtTuesdayHours"]').val((new Number($('input[id*="txtTuesdayHours"]').val())).toFixed(2)); 
        }, 750); 
       } 
      }); 

      var content3 = $('input[id*="txtWednesdayHours"]').val(); 
      $('input[id*="txtWednesdayHours"]').keyup(function() { 
       if ($('input[id*="txtWednesdayHours"]').val() != content3) { 

        content3 = $('input[id*="txtWednesdayHours"]').val(); 
        var hoursMon = new Number(content3); 
        var hoursTue = new Number($('input[id*="txtMondayHours"]').val()); 
        var hoursWed = new Number($('input[id*="txtTuesdayHours"]').val()); 
        var hoursThu = new Number($('input[id*="txtThursdayHours"]').val()); 
        var hoursFri = new Number($('input[id*="txtFridayHours"]').val()); 
        var hoursSat = new Number($('input[id*="txtSaturdayHours"]').val()); 
        var hoursSun = new Number($('input[id*="txtSundayHours"]').val()); 

        $('input[id*="txtTotalWorkingHours"]').val(parseFloat(hoursMon.toFixed(2)) + parseFloat(hoursTue.toFixed(2)) + parseFloat(hoursWed.toFixed(2)) + parseFloat(hoursThu.toFixed(2)) + parseFloat(hoursFri.toFixed(2)) + parseFloat(hoursSat.toFixed(2)) + parseFloat(hoursSun.toFixed(2))); 

        delay(function() { 
         $('input[id*="txtWednesdayHours"]').val((new Number($('input[id*="txtWednesdayHours"]').val())).toFixed(2)); 
        }, 750); 
       } 
      }); 

      var content4 = $('input[id*="txtThursdayHours"]').val(); 
      $('input[id*="txtThursdayHours"]').keyup(function() { 
       if ($('input[id*="txtThursdayHours"]').val() != content4) { 

        content4 = $('input[id*="txtThursdayHours"]').val(); 
        var hoursMon = new Number(content4); 
        var hoursTue = new Number($('input[id*="txtMondayHours"]').val()); 
        var hoursWed = new Number($('input[id*="txtTuesdayHours"]').val()); 
        var hoursThu = new Number($('input[id*="txtWednesdayHours"]').val()); 
        var hoursFri = new Number($('input[id*="txtFridayHours"]').val()); 
        var hoursSat = new Number($('input[id*="txtSaturdayHours"]').val()); 
        var hoursSun = new Number($('input[id*="txtSundayHours"]').val()); 

        $('input[id*="txtTotalWorkingHours"]').val(parseFloat(hoursMon.toFixed(2)) + parseFloat(hoursTue.toFixed(2)) + parseFloat(hoursWed.toFixed(2)) + parseFloat(hoursThu.toFixed(2)) + parseFloat(hoursFri.toFixed(2)) + parseFloat(hoursSat.toFixed(2)) + parseFloat(hoursSun.toFixed(2))); 

        delay(function() { 
         $('input[id*="txtThursdayHours"]').val((new Number($('input[id*="txtThursdayHours"]').val())).toFixed(2)); 
        }, 750); 
       } 
      }); 

      var content5 = $('input[id*="txtFridayHours"]').val(); 
      $('input[id*="txtFridayHours"]').keyup(function() { 
       if ($('input[id*="txtFridayHours"]').val() != content5) { 

        content5 = $('input[id*="txtFridayHours"]').val(); 
        var hoursMon = new Number(content5); 
        var hoursTue = new Number($('input[id*="txtMondayHours"]').val()); 
        var hoursWed = new Number($('input[id*="txtWednesdayHours"]').val()); 
        var hoursThu = new Number($('input[id*="txtThursdayHours"]').val()); 
        var hoursFri = new Number($('input[id*="txtTuesdayHours"]').val()); 
        var hoursSat = new Number($('input[id*="txtSaturdayHours"]').val()); 
        var hoursSun = new Number($('input[id*="txtSundayHours"]').val()); 

        $('input[id*="txtTotalWorkingHours"]').val(parseFloat(hoursMon.toFixed(2)) + parseFloat(hoursTue.toFixed(2)) + parseFloat(hoursWed.toFixed(2)) + parseFloat(hoursThu.toFixed(2)) + parseFloat(hoursFri.toFixed(2)) + parseFloat(hoursSat.toFixed(2)) + parseFloat(hoursSun.toFixed(2))); 

        delay(function() { 
         $('input[id*="txtFridayHours"]').val((new Number($('input[id*="txtFridayHours"]').val())).toFixed(2)); 
        }, 750); 
       } 
      }); 

      var content6 = $('input[id*="txtSaturdayHours"]').val(); 
      $('input[id*="txtSaturdayHours"]').keyup(function() { 
       if ($('input[id*="txtSaturdayHours"]').val() != content6) { 

        content6 = $('input[id*="txtSaturdayHours"]').val(); 
        var hoursMon = new Number(content6); 
        var hoursTue = new Number($('input[id*="txtMondayHours"]').val()); 
        var hoursWed = new Number($('input[id*="txtWednesdayHours"]').val()); 
        var hoursThu = new Number($('input[id*="txtThursdayHours"]').val()); 
        var hoursFri = new Number($('input[id*="txtFridayHours"]').val()); 
        var hoursSat = new Number($('input[id*="txtTuesdayHours"]').val()); 
        var hoursSun = new Number($('input[id*="txtSundayHours"]').val()); 

        $('input[id*="txtTotalWorkingHours"]').val(parseFloat(hoursMon.toFixed(2)) + parseFloat(hoursTue.toFixed(2)) + parseFloat(hoursWed.toFixed(2)) + parseFloat(hoursThu.toFixed(2)) + parseFloat(hoursFri.toFixed(2)) + parseFloat(hoursSat.toFixed(2)) + parseFloat(hoursSun.toFixed(2))); 

        delay(function() { 
         $('input[id*="txtSaturdayHours"]').val((new Number($('input[id*="txtSaturdayHours"]').val())).toFixed(2)); 
        }, 750); 
       } 
      }); 

      var content7 = $('input[id*="txtSundayHours"]').val(); 
      $('input[id*="txtSundayHours"]').keyup(function() { 
       if ($('input[id*="txtSundayHours"]').val() != content7) { 

        content7 = $('input[id*="txtSundayHours"]').val(); 
        var hoursMon = new Number(content7); 
        var hoursTue = new Number($('input[id*="txtMondayHours"]').val()); 
        var hoursWed = new Number($('input[id*="txtWednesdayHours"]').val()); 
        var hoursThu = new Number($('input[id*="txtThursdayHours"]').val()); 
        var hoursFri = new Number($('input[id*="txtFridayHours"]').val()); 
        var hoursSat = new Number($('input[id*="txtSaturdayHours"]').val()); 
        var hoursSun = new Number($('input[id*="txtTuesdayHours"]').val()); 

        $('input[id*="txtTotalWorkingHours"]').val(parseFloat(hoursMon.toFixed(2)) + parseFloat(hoursTue.toFixed(2)) + parseFloat(hoursWed.toFixed(2)) + parseFloat(hoursThu.toFixed(2)) + parseFloat(hoursFri.toFixed(2)) + parseFloat(hoursSat.toFixed(2)) + parseFloat(hoursSun.toFixed(2))); 

        delay(function() { 
         $('input[id*="txtSundayHours"]').val((new Number($('input[id*="txtSundayHours"]').val())).toFixed(2)); 
        }, 750); 
       } 
      }); 

     }); 
    </script> 
+0

전혀 관련이없는 메모입니다. 귀하의 코드를 DRYing하는 것이 좋습니다. 당신은 똑같은 일을 7 번하고, 그 과정에서, 당신은 또 다른 일을 각각 7 번합니다. – bstakes

답변

4

을하고 무엇을하고있는 underscore.js 라이브러리의 debounce function.

+0

그 라이브러리를 이용해 주셔서 감사합니다. 기존 jQuery 기능을 사용하고 있으므로 다니엘의 제안과 함께 결정했습니다. – Seany84