1. 程式人生 > >JS 自定義sleep以及Ajax 自執行函式

JS 自定義sleep以及Ajax 自執行函式

<script src="/static/js/jquery-2.1.1.min.js"></script>
    <script>
        function sleep(numberMillis) {
	        var now = new Date();
	        var exitTime = now.getTime() + numberMillis;
	        while (true) {
		    now = new Date();
		    if (now.getTime() > exitTime)
		    return;
	       }
            }
    </script>

 Ajax 自執行函式

        function ajax_send(func){
        var query = new Object();
            $.ajax({
                url:'/alert_view',
                type:'GET',
                data:'',
                success:function (data) {
                    var data = JSON.parse(data);
                    
for (key in data) { document.getElementById(key).style.height = data[key] + 'px'; } }) }

      //自執行函式 (function (data) { ajax_send(function (data) {
if (data.status==1){ console.log(data); } }); })();
     

      // 1.2s執行一次 setInterval(ajax_send,
1200) </script>