Created
April 8, 2014 11:28
-
-
Save nanasess/10111849 to your computer and use it in GitHub Desktop.
jQuery.when()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
var $message = $('#message'); | |
$('#execute').click(function() { | |
var customer_ids = $('#customer_ids').val().split(/\n/); | |
var promises = []; | |
$.each(customer_ids, function(i, customer_id) { | |
var d = $.Deferred(); | |
process(i, customer_id, $message, d); | |
promises.push(d); | |
}) | |
$.when.apply($, promises) | |
.done(function () { | |
var d = $.Deferred(); | |
var msg = '実行完了しました'; | |
alert(msg); | |
$message.text(msg); | |
d.resolve(); | |
return d; | |
}); | |
}); | |
}); | |
var process = function(i, customer_id, $message, d) { | |
var id = setTimeout(function() { | |
var | |
ORDER_KIND = [], | |
SEQ = [], | |
DETAIL_STARTDAY = [], | |
ITEM_ID = [], | |
ITEM_QTY = [], | |
DETAIL_NEXTDAY = [], | |
REGULAR_NUMBER = [], | |
WEBKBN = []; | |
var v = i % 2; | |
var stype = 1; | |
if (v == 0) { | |
stype = 1; | |
} else { | |
stype = 2; | |
} | |
$('input[id^=SEQ]').each(function() { | |
var rel = $(this).attr('rel'); | |
ORDER_KIND.push($('#ORDER_KIND_' + rel).val()); | |
SEQ.push($('#SEQ_' + rel).val()); | |
DETAIL_STARTDAY.push($('#DETAIL_STARTDAY_' + rel).val()); | |
ITEM_ID.push($('#ITEM_ID_' + rel).val()); | |
ITEM_QTY.push($('#ITEM_QTY_' + rel).val()); | |
DETAIL_NEXTDAY.push($('#DETAIL_NEXTDAY_' + rel).val()); | |
REGULAR_NUMBER.push($('#REGULAR_NUMBER_' + rel).val()); | |
WEBKBN.push($('#WEBKBN_' + rel).val()); | |
}); | |
var postData = { | |
CUSTOMER_WEBID: customer_id, | |
stype: stype, | |
ORDER_KIND: ORDER_KIND, | |
SEQ: SEQ, | |
DETAIL_STARTDAY: DETAIL_STARTDAY, | |
ITEM_ID: ITEM_ID, | |
ITEM_QTY: ITEM_QTY, | |
DETAIL_NEXTDAY: DETAIL_NEXTDAY, | |
REGULAR_NUMBER: REGULAR_NUMBER, | |
WEBKBN: WEBKBN | |
}; | |
var call = $('#call').val(); | |
if (call == undefined || call < 1) { | |
call = 1; | |
} | |
window.console.log('execute ' + i + ' JSSQL_GETCUST: ' + customer_id ); | |
$.ajax({ | |
type: 'POST', | |
url: "/shopping/admin/lockon/tools/wksMethods/JSSQL_GETCUST.php", | |
dataType: 'html', | |
data: {CUSTOMER_WEBID: customer_id}, | |
error: function(jqXHR, textStatus, errorThrown) { | |
window.console.log(errorThrown); | |
}, | |
success: function(data, textStatus, jqXHR) { | |
window.console.log('success JSSQL_GETCUST: ' + customer_id ); | |
} | |
}); | |
for (var n = 0; n < call; n++) { | |
window.console.log('execute ' + n + ' JSSQL_GETUNITPRICE: ' + customer_id ); | |
$.ajax({ | |
type: 'POST', | |
url: "<?php echo $_SERVER['PHP_SELF']; ?>", | |
dataType: 'html', | |
data: postData, | |
error: function(jqXHR, textStatus, errorThrown) { | |
window.console.log(errorThrown); | |
}, | |
success: function(data, textStatus, jqXHR) { | |
window.console.log('success JSSQL_GETUNITPRICE: ' + customer_id ); | |
} | |
}); | |
} | |
$message.text(customer_id + ' を実行しています'); | |
d.resolve(); | |
}, $('#interval').val() * 1000 * i); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see also.
http://foreignkey.jp/archives/1147