调用函数将返回“未定义" [英] Calling for a function returns 'undefined'

查看:160
本文介绍了调用函数将返回“未定义"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有一个表格.当用户单击发送"按钮时,它还会在后台通过调用一个函数来生成一个unique_number,该函数生成该数字,并在数据库中检查该数字是否不存在.如果可以-再次生成,如果不可以-返回此数字.但是,由于某种原因,当我试图将此数字打印到页面上或对其发出警报时,尽管该函数返回了数字,但我却得到了undefined的信息. 这是该函数的调用:

I have a form on my page. When the user hits the Send button, in the background it also generates a unique_number by calling for a function that generates this number and also checks in the DB that this number doesn't exist; if it does - generates it again, if doesn't - returns this number. However, for some reason when I'm trying to print out this number to the page, or alert it - I'm getting undefined, though the function returns the number. Here's the call for the function:

var unique_num = create_unique_number();
alert(unique_num);
    rest of this function...

这是函数本身:

function create_unique_number()
{
var num = Math.floor(Math.random() * (999999 - 100000 + 1)) + 100000;

$.getJSON("inc/API.php", 
{
    command:"is_unique_number_exist",
    unique_num:num
},
    function(result){
        if(result==true){
            create_unique_number();
        }
        else
        {
            return num;
        }
    });
}

如果为true,我会得到结果-如果为false,它会生成新的数字-应该返回.我尝试在else部分中将num编号为alert,它确实提醒了数字,但返回时-未定义. 为什么会发生以及如何解决?

I do get the results, if it's true - it generates new number, if false - should return. I tried to alert the num in the else part, and it did Alert the number, but on return - undefined. Why is it happening and how to fix it?

推荐答案

有关使用延迟对象的解决方案,请尝试以下操作:

For a solution using deferred objects, try this:

function create_unique_number()
{
    var num = Math.floor(Math.random() * (999999 - 100000 + 1)) + 100000;

    return $.getJSON("inc/API.php", {
        command:"is_unique_number_exist",
        unique_num:num
    }).then(function(result) {
        return result ? create_unique_number() : num;
    });
}

这当然未经测试,因为我没有您的API,但是理论是,如果resulttrue,则.then调用将返回递归调用的结果回到相同的功能.如果resultfalse,则它将返回选定的数字作为新的Promise(已解决)的结果.

This is of course untested as I don't have your API available, but the theory is that if result is true the .then call returns the result of a recursive call back to the same function. If result is false then it returns the chosen number as the (resolved) result of a new promise.

如果任何AJAX调用失败,则循环应会中断,您可以通过.fail调用来捕获它:

If any AJAX call fails, the loop should break and you can trap that with a .fail call:

create_unique_number().done(function(n) {
    // use your unique number "n" here
    var unique_num = n;
    ...
}).fail(function() {
    // there was an AJAX error
});

// can't use "unique_num" here - execution continues here immediately
// and "unique_num" isn't defined until the above ".done" callback is
// invoked some time later

请注意,递归调用不是 truly 递归-由于AJAX是事件驱动的,因此在AJAX .then事件触发之前很久,原始函数已经终止并清理了其堆栈.对create_unique_number的新呼叫将与原始呼叫处于相同的呼叫堆栈"级别.

Note that the recursive call isn't truly recursive - since AJAX is event driven, the original function will already have terminated and cleaned up its stack long before the AJAX .then event fires. The resulting new call to create_unique_number will be at the same "call stack" level as the original call.

此外,正如评论中指出的那样,让服务器为您提供随机数实际上要简单得多.

Also, as pointed out in the comments, it would actually be far simpler to have the server give you a random number.

这篇关于调用函数将返回“未定义"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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