使用Javascript函数调用jQuery函数

时间:2022-02-16 16:02:34

Having some trouble getting this to work, specifically with $.getJSON(). I want to wrap the getJSON function from jQuery in a Javascript function like so:

让它工作时遇到一些麻烦,特别是使用$ .getJSON()。我想在Javascript函数中包装来自jQuery的getJSON函数,如下所示:

function reload_data() {
    $.getJSON("data/source", function(data) {
        $.d = data;
    });
}

But when I call reload_data() it doesn't execute the jQuery function inside. Any ideas?

但是当我调用reload_data()时,它不会执行里面的jQuery函数。有任何想法吗?

3 个解决方案

#1


3  

You're not telling us enough. I will just take a guess!

你告诉我们的不够。我会猜一猜!

If you are calling this function, and then immediately checking $.d for the results, that is not going to work because you don't allow time for the asynchronous AJAX request to complete...

如果你正在调用这个函数,然后立即检查$ .d的结果,这是行不通的,因为你没有时间让异步AJAX请求完成...

reload_data();
alert($.d); // What?!  It's not displaying the updated results?!

You'll have to utilize a callback structure, like jQuery uses, in order to make it work...

你必须利用一个回调结构,比如jQuery使用,以使其工作......

reload_data(function() {
  alert($.d);
});

function reload_data(func) {
  $.getJSON("data/source", function(data) {
    $.d = data;
    //Execute the callback, now that this functions job is done
    if(func)
      func();
  });
}

#2


2  

Put an Alert in side the function to know its getting called.

在函数旁边放置一个警报,以了解其被调用。

and a try catch around the jQuery call to see if there is an error

并尝试捕获jQuery调用以查看是否存在错误

function reload_data() {
    alert('reload_data  start');
    try{
        $.getJSON("data/source", function(data) {
            $.d = data;
        });
     }
     catch (ex){
         alert ('error in jQuery call:' + ex)
     }
}

#3


0  

Thanks for the help everyone, but the solution was actually really simple. It was just a matter of synchronization. The page was reloading before the JSON data got reloaded, so that's why I wasn't seeing an error.

感谢大家的帮助,但解决方案实际上非常简单。这只是一个同步的问题。在重新加载JSON数据之前页面正在重新加载,这就是我没有看到错误的原因。

#1


3  

You're not telling us enough. I will just take a guess!

你告诉我们的不够。我会猜一猜!

If you are calling this function, and then immediately checking $.d for the results, that is not going to work because you don't allow time for the asynchronous AJAX request to complete...

如果你正在调用这个函数,然后立即检查$ .d的结果,这是行不通的,因为你没有时间让异步AJAX请求完成...

reload_data();
alert($.d); // What?!  It's not displaying the updated results?!

You'll have to utilize a callback structure, like jQuery uses, in order to make it work...

你必须利用一个回调结构,比如jQuery使用,以使其工作......

reload_data(function() {
  alert($.d);
});

function reload_data(func) {
  $.getJSON("data/source", function(data) {
    $.d = data;
    //Execute the callback, now that this functions job is done
    if(func)
      func();
  });
}

#2


2  

Put an Alert in side the function to know its getting called.

在函数旁边放置一个警报,以了解其被调用。

and a try catch around the jQuery call to see if there is an error

并尝试捕获jQuery调用以查看是否存在错误

function reload_data() {
    alert('reload_data  start');
    try{
        $.getJSON("data/source", function(data) {
            $.d = data;
        });
     }
     catch (ex){
         alert ('error in jQuery call:' + ex)
     }
}

#3


0  

Thanks for the help everyone, but the solution was actually really simple. It was just a matter of synchronization. The page was reloading before the JSON data got reloaded, so that's why I wasn't seeing an error.

感谢大家的帮助,但解决方案实际上非常简单。这只是一个同步的问题。在重新加载JSON数据之前页面正在重新加载,这就是我没有看到错误的原因。