DjangoをベースにしたWebアプリケーションがあります。ScrapyCrawlerを使用してWebページをクロールします。現時点での私の目標は、jQueryおよびAJAXリクエストを使用してWebページ内からクローラーを制御できるようにすることです。
私の理論的な設定は次のとおりです。
- ウェブページにボタンがあります。ボタンをクリックすると、サーバー側でクローラーが起動します。
window.setInterval
クローラーが起動したら、これまでにクロールされたWebページの数を確認するために、定期的にAJAXGETリクエストをサーバーに送信します。- クローラーが終了したら、GETリクエストはを使用して停止する必要があります
window.clearInterval
。
これらは私の現在のコードからの関連する行です:
$(document).ready(function() {
// This variable will hold the ID returned by setInterval
var monitorCrawlerId;
$startCrawlerButton.on('click', function(event) {
// This function should be run periodically using setInterval
var monitorCrawler = function() {
$.ajax({
type: 'GET',
url: '/monitor_crawler/',
// ...
success: function(response) {
// if the server sends the message that the crawler
// has stopped, use clearInterval to stop executing this function
if (response.crawler_status == 'finished') {
clearInterval(monitorCrawlerId);
}
}
});
};
// Here I send an AJAX POST request to the server to start the crawler
$.ajax({
type: 'POST',
url: '/start_crawler/',
// ...
success: function(response) {
// If the form that the button belongs to validates correctly,
// call setInterval with the function monitorCrawler defined above
if (response.validation_status == 'success') {
monitorCrawlerId = setInterval('monitorCrawler()', 10000);
}
}
});
});
});
問題:このコードを実行すると、FirefoxのWebコンソールで次のようになります。
ReferenceError: monitorCrawler is not defined
ただし、奇妙なことに、関数monitorCrawler
はとにかく定期的に実行されます。しかし、実行するたびに、同じエラーメッセージが再び表示されます。monitorCrawler
外に置い$startCrawlerButton.on()
ても同じエラーが発生します。どうすればこれを解決できますか?私はJavaScriptの初心者なので、どんな助けでも大歓迎です。どうもありがとうございます!