jQuery.load()不能用定义的函数作为回调吗? [英] jQuery.load() doesn't work with a defined function as a callback?

查看:63
本文介绍了jQuery.load()不能用定义的函数作为回调吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释为什么我在这段代码中遇到竞争条件:

Can someone explain to me why I was running across a race condition with this block of code:

$("#someid").load( "home.php", callback_function );
function callback_function()
{
    //some jQuery.get() goes here
}

而不是这段代码:

$("#someid").load( "home.php", function(){ callback_function(); } );
function callback_function()
{
    //some jQuery.get() goes here
}

第一个代码块中的竞争条件是callback_function()中的处理在加载完成后不会执行,但似乎是异步运行并且会搞砸一些UI内容(这个UI的东西依赖于jQuery.get()返回)。我是jQuery / JavaScript的新手,想知道为什么我需要显式的function(){callback_function();}。谢谢!

The race condition in the first block of code was that processing in callback_function() would not execute after the load was done, but seemed to run asynchronously and would screw up some UI stuff (this UI stuff was dependent on the jQuery.get() returning). I'm new to jQuery/JavaScript and was wondering why I need the explicit "function(){ callback_function(); }". Thanks!

推荐答案

查看在变量内发送一个jquery回调函数

声明函数如

function callback_function(){
  //some jQuery.get() goes here
}

var callback_function = function(){
  //some jQuery.get() goes here
}

然后试试这个

$("#someid").load( "home.php",null, callback_function );

将callback参数作为第三个参数传递,以避免混淆jquery加载API(抱歉没有得到确切的单词:))。

pass the callback parameter as 3rd argument to avoid confusion to the jquery load API(sorry not getting exact word :)).

请参阅加载文档

这篇关于jQuery.load()不能用定义的函数作为回调吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆