Underscore.js 函數節流簡單測試

函數節流在平常的DOM界面數據交互中有比較大的做用,能夠減小服務器的請求,同時減小客戶端的內存影響javascript

Underscore.js  本省就包含了函數節流的處理函數 html

_.throttle 和 _.debounce java

簡單的測試使用以下:jquery

須要使用的類庫爲jquery  、Underscore ajax

測試的方法爲:mousemove 事件服務器

測試頁面代碼以下:app

<!DOCTYPE html >
<html>
<head>
    <script src="jquery-1.11.1.min.js" type="text/javascript"></script>
    <script src="underscore-min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#infoapp').on('mousemove', alertinfo);  不使用節流方法

         //   $('#infoapp').on('mousemove', _.throttle(alertinfo, 5000));    使用節流方法  throttle

         //   $('#infoapp').on('mousemove', _.debounce(alertinfo, 1000, false));    使用節流方法debounce
        }
        )
       //  進行回調的事件處理函數
        function alertinfo() {

            var data = new Date();
            console.log(data);
        }
    </script>
</head>
<body>
    <div id="infoapp" style="background-color: Red; width: 200px; height: 200px;   margin:0 auto;">
    </div>
</body>
</html>

測試的結果以下:函數

 

結論:測試

 總的來講對於咱們在密集數據請求的ajax 交互中使用函數節流的方式有很大的幫助,減小了不少的沒有必要的數據請求。spa

相關文章
相關標籤/搜索