简体   繁体   English

在Ajax内部调用一个函数

[英]Call a function inside Ajax

I'm having a problem, I can't make a loop inside this code: 我遇到问题,无法在此代码内循环:

    class ProductsPager
      constructor: ->
        this.waitformsg()
      waitformsg: =>
        alert 'begin'
        $.ajax
          type: "GET"
          url: "http://0.0.0.0:3000/live/index.json"
          async: true
          cache: false
          timeout: 1000
          success: (data) ->
            alert data
          error: (XMLHttpRequest, textStatus, errorThrown) ->
            alert "end"
            waitformsg()setTimeout "waitformsg()", 0

The two arlerts are for debugging. 这两个警报器用于调试。 It displays me only one time of each: " begin ", and right after " end " and nothing else. 它只显示一次:“ 开始 ”,仅显示“ 结束 ”之后,仅显示一次。
I have concluded that the last line is wrong and I need to find a way to call a method inside Ajax. 我已经得出结论,最后一行是错误的,我需要找到一种在Ajax中调用方法的方法。
I have tried to replace setTimeout "waitformsg()", 0 by this.waitformsg() or even waitformsg() but it still doesn't work. 我试图用this.waitformsg()甚至waitformsg()替换setTimeout "waitformsg()", 0 ,但是它仍然无法正常工作。

I would like to display infinite "alerts" untill the right conditions to succeed are gathered. 我想展示无限的“警报”,直到收集成功的正确条件。

These two: 这两个:

waitformsg()
setTimeout waitformsg, 0

won't work because there is no waitformsg function in scope. 无法使用,因为范围中没有waitformsg函数。

This: 这个:

setTimeout "waitformsg()", 0

won't work because there is no global function called waitformsg , the string form of setTimeout executes the string in the global context. 由于没有名为waitformsg 全局函数,因此不会工作, setTimeout的字符串形式将在全局上下文中执行该字符串。 I'd recommend that you forget that setTimeout even has a string form. 我建议您忘记setTimeout甚至具有字符串形式。

You should bind the callbacks to the current context using a fat arrow ( => ) : 您应该使用粗箭头( =>将回调绑定到当前上下文:

waitformsg: =>
  alert 'begin'
  $.ajax
    #...
    error: (XMLHttpRequest, textStatus, errorThrown) =>
      alert "end"
      @waitformsg()

And if you want the error handler to wait a second before trying again: 如果您希望错误处理程序等待一秒钟再尝试:

setTimeout @waitformsg, 1000

The waitformsg method will run in the correct context because you defined it as a bound function. 因为您waitformsg方法定义为绑定函数,所以它将在正确的上下文中运行。

A couple more things while I'm here: 我在这里的其他几件事:

  1. Using @ is more idiomatic in CoffeeScript than using this so your constructor should be: 使用@在CoffeeScript中更地道比使用this所以你的构造应该是:

     constructor: -> @waitformsg() 
  2. jQuery's async:true flag for $.ajax has been deprecated so you should stop using it. jQuery的$.ajax标志async:true已被弃用,因此您应该停止使用它。 async:true is also a nasty thing to do to your users (especially in a loop) so again, you should stop using it. 对于用户(尤其是在循环中), async:true也是一件令人讨厌的事情,因此,再次,您应该停止使用它。

I haven't played with CoffeeScript much, so here's how you would do it in Javascript: 我没怎么玩CoffeeScript,所以这是您在Javascript中的处理方式:

waitformsg: function() {
    var self = this;

    ...
    ...

    error: function(XMLHttpRequest, textStatus, errorThrown) {
        self.waitformsg();
    };
};

When you simply call waitformsg() , you're basically calling window.waitformsg() . 当您简单地调用waitformsg() ,您基本上是在调用window.waitformsg() Since this doesn't exist, your code doesn't work. 由于不存在,因此您的代码不起作用。 You cannot use this , because the callback is asynchronous and so the value of this is pretty much whatever jQuery sets it to (if it sets it at all) when the error callback is called. 你不能用this ,因为回调是异步的,这样的价值this是几乎任何的jQuery将其设置为(如果它设置它在所有)时, error回调被调用。 So you need to maintain a reference to this that gets passed down into the close as well and you can do this by creating a new variable in the local scope of the function and assigning this to that variable (in this case, I've called that new variable self ). 所以,你需要保持一个参考this是被流传下来到靠近酒店,你可以通过在函数的局部范围内建立一个新的变量和分配做this该变量(在这种情况下,我已经叫该新变量self )。 Then you can call self.waitformsg(); 然后可以调用self.waitformsg(); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM