Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何解决此引用错误?_Javascript_Jquery - Fatal编程技术网

Javascript 如何解决此引用错误?

Javascript 如何解决此引用错误?,javascript,jquery,Javascript,Jquery,我有一个基于Django的web应用程序。我使用爬网来抓取网页。目前,我的目标是能够使用jQuery和AJAX请求从网页中控制爬虫程序 我的理论设置如下: 在网页上,我有一个按钮。当我单击按钮时,爬虫程序在服务器端启动。 一旦爬虫程序启动,我会定期使用向服务器发送AJAX GET请求,以了解到目前为止爬虫了多少网页。 一旦爬虫程序完成,GET请求应该通过使用停止。 以下是我当前代码中的相关行: $(document).ready(function() { // This variabl

我有一个基于Django的web应用程序。我使用爬网来抓取网页。目前,我的目标是能够使用jQuery和AJAX请求从网页中控制爬虫程序

我的理论设置如下:

在网页上,我有一个按钮。当我单击按钮时,爬虫程序在服务器端启动。 一旦爬虫程序启动,我会定期使用向服务器发送AJAX GET请求,以了解到目前为止爬虫了多少网页。 一旦爬虫程序完成,GET请求应该通过使用停止。 以下是我当前代码中的相关行:

$(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新手,所以非常感谢您的帮助。多谢各位

试试看

monitorCrawlerId = setInterval(monitorCrawler, 10000);
带参数:

monitorCrawlerId = setInterval(function(){
      //prepare params.
      monitorCrawler(/* param1, param2*/);
   }, 10000);
试一试

带参数:

monitorCrawlerId = setInterval(function(){
      //prepare params.
      monitorCrawler(/* param1, param2*/);
   }, 10000);
改变

永远不要将字符串传递给,而是传递函数引用!它们每次都会被求值,而且是在全局范围内,但是你的monitorCrawler函数对于click处理程序来说是本地的,我想把它放在外部意味着进入ready回调。

Change

setInterval('monitorCrawler()', 10000);

永远不要将字符串传递给,而是传递函数引用!它们每次都会被求值,而且是在全局范围内——但是您的monitorCrawler函数对于click处理程序来说是本地的,我猜,如果将它放在ready回调函数的外部,就意味着要将它放入ready回调函数中。

setInterval,当第一个参数是string时,会在全局窗口上下文中解析。您可以给它一个指向要调用的函数的变量,甚至可以:

setInterval(function(){monitorCrawler();}, 10000);
这将创建一个闭包,当触发interval时,局部变量monitorCrawler仍将存在。

setInterval,当第一个参数为string时,将在全局窗口上下文中解析。您可以给它一个指向要调用的函数的变量,甚至可以:

setInterval(function(){monitorCrawler();}, 10000);

这将创建一个闭包,当触发interval时,局部变量monitorCrawler将仍然存在。

好的,这可以工作,但这样我就不能将任何参数传递给monitorCrawler,对吗?我实际上需要在我的代码中这样做,但为了保持简单,我的问题中没有这样做。请看我的答案,使用匿名函数作为第一个参数。您可以创建@Igor所说的匿名函数。很酷,@Igor的版本正是我所需要的。我认为他的回答是被接受的,因为他比你贴得早,尤里。无论如何,非常感谢@彼得斯塔尔:不客气。请记住,永远不要再像Bergi指出的那样将代码传递给setInterval好的,这是可行的,但这样我就不能将任何参数传递给monitorCrawler,对吗?我实际上需要在我的代码中这样做,但为了保持简单,我的问题中没有这样做。请看我的答案,使用匿名函数作为第一个参数。您可以创建@Igor所说的匿名函数。很酷,@Igor的版本正是我所需要的。我认为他的回答是被接受的,因为他比你贴得早,尤里。无论如何,非常感谢@彼得斯塔尔:不客气。请记住,永远不要再像Bergi指出的那样将代码传递给setInterval谢谢,伊戈尔这正是我需要的。通过这种方式,我可以向monitorCrawler添加参数,这在我的案例中是必要的。由于您是第一个发布此特定解决方案的人,我接受您的回答。谢谢,伊戈尔:这正是我需要的。通过这种方式,我可以向monitorCrawler添加参数,这在我的案例中是必要的。由于您是第一个发布此特定解决方案的人,我接受您的回答。