#実現したいこと
前回取得したdataの数とajaxで再度取得したデーターの数を比較して、データー量が増えた場合のみリロード処理を走らせたい。
#困っていること
前回取得したデータの数がajaxを走らせる度になくなってしまうので、ajaxで取得したデーターの数と比較することができない。
js
1$(function() { 2 var POLLLING_INVERVAL_TIME_IN_MILLIS = 10000;//10s 3 (function polling() { 4 if(!document.hidden) { // このページが表示されているときだけリクエストする 5 getCountUp(); 6 } 7 window.setTimeout(polling, POLLLING_INVERVAL_TIME_IN_MILLIS); 8 }()); 9 10 11 function getCountUp() { 12 let show_id = document.getElementById("show_id").value; 13 $.ajax({ 14 url: `/show/${show_id}`, 15 type: "GET", 16 data: {memos: 'text'}, 17 dataType: "json", 18 success: function (data) { 19 //前回取得したdataの数とajaxで再度取得したデーターの数を比較して、データー量が増えた場合のみリロード処理を走らせたい。 20 let data_count = data.length; 21 let get_data_count = data.length; 22 if( data_count < get_data_count){ 23 location.reload(); 24 } 25 }, 26 error: function (data) { 27 console.log("error"); 28 } 29 }); 30 } 31});
#追記
js
1 function getCountUp() { 2 let room_id = document.getElementById("room_id").value; 3 let data_count = document.getElementById("data_count").value; 4 $.ajax({ 5 url: `/rooms/${room_id}`, 6 type: "GET", 7 //GETでpostしたい。 8 data: {memos: 'text', count: data_count}, 9 dataType: "json", 10 success: function (data) { 11 let data_count = document.getElementById("data_count").value; 12 let get_data_count = data.length; 13 $('#data_count').val(data.length); 14 }, 15 error: function (data) { 16 console.log("error"); 17 } 18 }); 19 }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/25 01:06
退会済みユーザー
2019/08/25 01:15
2019/08/25 01:41
2019/08/25 01:47