【问题标题】:passing argument to callback function listening on argumnets将参数传递给监听参数的回调函数
【发布时间】:2020-02-12 15:27:38
【问题描述】:
function setmclisten(message, sender, sendResponse) {
  console.log(data);
  if(message['type'] === 'startUp')
  {
    console.log(data);
    sendResponse(data)
  }
}
function QuarryToServer(){
  chrome.runtime.onMessage.removeListener(setmclisten);
  $.ajax({
    type: "GET",
    async: true,
    form: 'formatted',
    url: SERVERURL,
    success: function (data) {
      //sends a get 
      console.log("set startup listener");
      debugger;
      chrome.runtime.onMessage.addListener(setmclisten);
    },
    fail: function () { console.error("error quarrying server"); }
  });
}

我遇到的问题是我需要命名函数,以便稍后删除侦听器,但是当我将其设为命名函数时,我无法访问数据变量,并且如果我尝试像 addListen(func(args) 一样传递它) 它只会调用函数而不是将其作为变量传递有没有办法传递变量,同时仍然在全局范围内定义函数 澄清一下:所以有 setmclisten,我需要它是一个命名函数,同时传递数据参数并接收 onmessge 侦听器参数,如它自己的消息

【问题讨论】:

  • "...当我将其设为命名函数时,我无法访问数据变量..." 为什么不呢?你说的是什么功能?
  • 所以有 setmclisten,我需要它是一个命名函数,同时传递数据参数并接收 onmessge 侦听器参数,如它自己的消息

标签: javascript ajax callback


【解决方案1】:

我想我看到了问题所在。有了更多的上下文,我们也许可以帮助您以更好的方式解决它,但最小的更改方法是记住最后一个侦听器,如下所示(参见*** cmets):

function setmclisten(message, sender, sendResponse, data) { // *** Note `data` param
                                                            // at end
  console.log(data);
  if(message['type'] === 'startUp')
  {
    console.log(data);
    sendResponse(data)
  }
}
let lastListener = null; // *** Remember the last listener
function QuarryToServer(){
  // *** Remove the last listener if any
  if (lastListener) {
      chrome.runtime.onMessage.removeListener(lastListener);
      lastListener = null;
  }
  $.ajax({
    type: "GET",
    async: true,
    form: 'formatted',
    url: SERVERURL,
    success: function (data) {
      //sends a get 
      console.log("set startup listener");
      // *** Create a new listener and attach it
      lastListener = function(message, sender, sendResponse) {
          return setmclisten(message, sender, sendResponse, data);
          // *** Or if `this` is important in the call:
          // return setmclisten.call(this, message, sender, sendResponse, data);
      };
      chrome.runtime.onMessage.addListener(lastListener);
    },
    fail: function () { console.error("error quarrying server"); }
  });
}

或者,始终附加一个侦听器而不是添加和删除它,并让它使用最新数据:

let lastData = null; // ***
function setmclisten(message, sender, sendResponse) {
  if (!lastData) {
    return;
  }
  console.log(lastData);
  if(message['type'] === 'startUp')
  {
    console.log(lastData);
    sendResponse(lastData)
  }
}
function QuarryToServer(){
  $.ajax({
    type: "GET",
    async: true,
    form: 'formatted',
    url: SERVERURL,
    success: function (data) {
      lastData = data; // ***
    },
    fail: function () { console.error("error quarrying server"); }
  });
}

在上面,我假设你这样做

chrome.runtime.onMessage.addListener(setmclisten);

一次。

【讨论】:

    猜你喜欢
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    相关资源
    最近更新 更多