Jqueryを使ってAjaxスレッドを複数同時並列で実行し、同時実行する数と順番を制御する簡単な方法

単発でAjaxを実行する

もっとも簡単なパターン
指定したURLにリクエストし、レスポンスを<span id=”result”></span>の中に表示する。

<html>
<body>
<span id="result"></span>
<script src="https://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
i=0;
threadcall(i);
function threadcall(i){
	(function(){
		$.ajax({
			url: 'https://example.com/example.php?str=hoge'+i,
			type: 'get',
			dataType: 'html'
		}).done(function(data){
			$('#result').append('<div>'+data+'</div>');
		}).fail(function(data){
			$('#result').append('error');
		});
	})();
}
</script>
</body>
</html>

ここから先は

3,257字

¥ 190

この記事が気に入ったらサポートをしてみませんか?