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

查看:96
本文介绍了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!

推荐答案

签出声明类似

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天全站免登陆