Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.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 如何在事件队列中进行异步jsonp调用_Javascript_Jquery_Asynchronous_Jsonp - Fatal编程技术网

Javascript 如何在事件队列中进行异步jsonp调用

Javascript 如何在事件队列中进行异步jsonp调用,javascript,jquery,asynchronous,jsonp,Javascript,Jquery,Asynchronous,Jsonp,关于如何进行异步JSONP调用的问题已经被问了好几次。在我的例子中,我需要在事件队列中使用此功能。我使用jQuery.bind-first将事件处理程序放到队列的开头。我需要在处理程序中进行jsonp调用,并在后续的用户定义处理程序中使用该调用的结果 编辑以澄清问题: 如何在事件处理程序中发出异步ajax jsonp请求,确保在ajax请求完成之前不会执行事件队列的其余部分 编辑: 我刚刚找到了几乎相同的。在这种方法中,像表单提交这样的事件发生是因为调用操作的表单上的最终提交事件不在事件队列中。

关于如何进行异步JSONP调用的问题已经被问了好几次。在我的例子中,我需要在事件队列中使用此功能。我使用jQuery.bind-first将事件处理程序放到队列的开头。我需要在处理程序中进行jsonp调用,并在后续的用户定义处理程序中使用该调用的结果

编辑以澄清问题: 如何在事件处理程序中发出异步ajax jsonp请求,确保在ajax请求完成之前不会执行事件队列的其余部分

编辑:

我刚刚找到了几乎相同的。在这种方法中,像表单提交这样的事件发生是因为调用操作的表单上的最终提交事件不在事件队列中。

答案非常简单。使用$.event.trigger创建新的事件队列,将当前处理程序之后的所有事件处理程序绑定到新事件,停止当前事件的传播,在jsonp调用成功时触发新事件

// Bind our function to the event name
$('.selector').bindFirst(evnt_nm, {}, function(e){
  var that = this

  // Stop all propagation from this event queue
  e.preventDefault()
  if (e.stopImmediatePropagation) e.stopImmediatePropagation()
  if (e.cancelBubble!=null) e.cancelBubble = true
  e.stopPropagation()

  // Create the new my-queue event
  $.event.trigger({
    type: "my-queue",
    message: e,
    time: new Date()
  });

  // Copy this events queue
  // Using slice(0) creates a copy instead of a reference
  var events = $._data( $(this)[0] ).events[evnt_nm].slice(0)

  // Remove this event from the queue
  events.shift()

  // Bind the event handlers to the new queue
  $.each(events, function(index, event){
    $(that).bind('my-queue',event.handler)
  })

  // Make your jsonp call
  $.ajax({
    // ...
    success : function(data, textStatus, jqXHR){
      // ...
      // Trigger the rest of the events stored in the new queue once your finished.
      $(that).trigger('my-queue')
    }
  })

  return false;
})

现在,当您的事件被触发时,队列中的其余事件将不会被处理,直到jsonp调用成功返回。

可能重复@acrosman,但极其相似的是,该答案不会在任何地方引用jsonp,因此在google搜索“异步jsonp”时不会出现。也许有了这个答案,其他人不会像我一样花一天的时间寻找答案。