条件为真时调用函数

时间:2016-11-07 14:14:40

标签: javascript function

当上述查询的结果为true时,我在调用倒计时函数时遇到了一些麻烦。基本上,脚本会检查用户是否具有权限并显示消息(如果有),然后倒计时并将其重定向到新页面。我通过语法检查器运行代码,它出来有效。

但是在检查浏览器调试器时,我得到错误'倒计时'是未定义的。不太清楚我在这里做错了什么。

function validate() {
    var userName = $().SPServices.SPGetCurrentUser({
        fieldName: "UserName"
    });
    var query = '<Query>' +
        '<Where>' +
        '<Eq>' +
        '<FieldRef Name="userid" />' +
        '<Value Type="User">' + userName + '</Value>' +
        '</Eq>' +
        '</Where>' +
        '</Query>';

    $(document).ready(function() {
        $().SPServices({

            operation: "GetListItems",
            async: false,
            listName: "test",
            CAMLViewFields: "<ViewFields><FieldRef Name='userid' /></ViewFields>",
            completefunc: function(xData, Status) {

                var matchFound = false; //define flag


                $(xData.responseXML).SPFilterNode("z:row").each(function() {
                    if (userName == $(this).attr("ows_userid")) {
                        matchFound = true; //set the flag to true indicating matched record
                    }

                    function countdown() {

                        var time_left = 5;
                        var cinterval;
                        var timestatus = 1;
                        var targetURL = "http://www.google.com"

                        function time_dec() {
                            time_left--;
                            document.getElementById('countdown').innerHTML = time_left;
                            if (time_left == 0) {
                                clearInterval(cinterval);
                                window.location = targetURL
                            }
                        }

                        function defaultstart() {
                            time_left = 5;
                            clearInterval(cinterval);
                            cinterval = setInterval('time_dec()', 1000);
                        }

                        defaultstart();
                    }
                });
                if (matchFound)
                    $('#validmsg').css('visibility', 'visible');

                else
                    $('#denymsg').css('visibility', 'visible');
                if (matchFound) {
                    countdown()
                }
            }
        });
    });
}

1 个答案:

答案 0 :(得分:0)

超出范围。如果要从外部调用它,则应在循环外定义countdown()。除了我没有看到需要把它放在里面,你似乎没有使用任何xData。

var matchFound = false;
function countdown() {
    var time_left = 5;
    var cinterval;
    var timestatus = 1;
    var targetURL = "http://www.google.com"

    function time_dec() {
        time_left--;
        document.getElementById('countdown').innerHTML = time_left;
        if (time_left == 0) {
            clearInterval(cinterval);
            window.location = targetURL
        }
    }

    function defaultstart() {
        time_left = 5;
        clearInterval(cinterval);
        cinterval = setInterval('time_dec()', 1000);
    }

    defaultstart();
}
$(xData.responseXML).SPFilterNode("z:row").each(function() {
    if (userName == $(this).attr("ows_userid")) {
        matchFound = true; //set the flag to true indicating matched record
    }
});
if (matchFound) {
    $('#validmsg').css('visibility', 'visible');
    countdown();
} else {
    $('#denymsg').css('visibility', 'visible');     
}