三元操作符条件永远不为真,尝试多行ajax请求

Ternary operator condition is never true, trying multiline ajax request

本文关键字:请求 ajax 三元 操作符 永远 条件      更新时间:2023-09-26

我正在创建一个老虎机。三元运算符总是假的,所以ajax请求$.ajax("delpoint.php")工作,但$.ajax("addpoint")不能。我可以肯定的是,错误在这一部分:

Javascript代码片段:

function check(){
    $msg.html(
        r[0] === r[1] && r[1] === r[2] ?
            'You won! Enjoy your ' + reels[1][ (r[0] / 70 + 1) % 3 | 0 ].split(' ')[0]
            var jqxhr = $.ajax( "addpoint.php")
        :
            'Try again'
            var jqxhr = $.ajax( "delpoint.php") 
    );
}

下面是完整的代码:

/*
    requestAnimationFrame polyfill
*/
(function(w){
    var lastTime = 0,
        vendors = ['webkit', /*'moz',*/ 'o', 'ms'];
    for (var i = 0; i < vendors.length && !w.requestAnimationFrame; ++i){
        w.requestAnimationFrame = w[vendors[i] + 'RequestAnimationFrame'];
        w.cancelAnimationFrame = w[vendors[i] + 'CancelAnimationFrame']
            || w[vendors[i] + 'CancelRequestAnimationFrame'];
    }
    if (!w.requestAnimationFrame)
        w.requestAnimationFrame = function(callback, element){
            var currTime = +new Date(),
                timeToCall = Math.max(0, 16 - (currTime - lastTime)),
                id = w.setTimeout(function(){ callback(currTime + timeToCall) }, timeToCall);
            lastTime = currTime + timeToCall;
            return id;
        };
    if (!w.cancelAnimationFrame)
        w.cancelAnimationFrame = function(id){
        clearTimeout(id);
    };
})(this);
/*
    Slot Machine
*/
var sm = (function(undefined){
    var tMax = 3000, // animation time, ms
        height = 210,
        speeds = [],
        r = [],
        reels = [
            ['coffee maker',   'teapot',       'espresso machine'],
            ['coffee filter',  'tea strainer', 'espresso tamper'],
            ['coffee grounds', 'loose tea',    'ground espresso beans']
        ],
        $reels, $msg,
        start;
    function init(){
        $reels = $('.reel').each(function(i, el){
            el.innerHTML = '<div><p>' + reels[i].join('</p><p>') + '</p></div><div><p>' + reels[i].join('</p><p>') + '</p></div>'
        });
        $msg = $('.msg');
        $('button').click(action);
    }
    function action(){
        if (start !== undefined) return;
        for (var i = 0; i < 3; ++i) {
            speeds[i] = Math.random() + .5; 
            r[i] = (Math.random() * 3 | 0) * height / 3;
        }
        $msg.html('Spinning...');
        animate();
    }
    function animate(now){
        if (!start) start = now;
        var t = now - start || 0;
        for (var i = 0; i < 3; ++i)
            $reels[i].scrollTop = (speeds[i] / tMax / 2 * (tMax - t) * (tMax - t) + r[i]) % height | 0;
        if (t < tMax)
            requestAnimationFrame(animate);
        else {
            start = undefined;
            check();
        }
    }
    function check(){
        $msg.html(
            r[0] === r[1] && r[1] === r[2] ?
                'You won! Enjoy your ' + reels[1][ (r[0] / 70 + 1) % 3 | 0 ].split(' ')[0]
                var jqxhr = $.ajax( "addpoint.php")
            :
                'Try again'
                var jqxhr = $.ajax( "delpoint.php") 
        );
    }
    return {init: init}
})();
$(sm.init);

试试这个:

function check() {
  $msg.html(r[0] === r[1] && r[1] === r[2] ?
    ('You won! Enjoy your ' + reels[1][(r[0] / 70 + 1) % 3 | 0].split(' ')[0],
    jqxhr = $.ajax("addpoint.php"), console.log("Enters"))
    : 
    ('Try again',
    jqxhr = $.ajax("delpoint.php"))
  );
}

它为我工作,我发现的唯一问题是抛出异常SyntaxError: missing : in conditional expression var jqxhr = $.ajax( "addpoint.php")

这意味着您没有使用逗号操作符来拆分三元操作符中的两个句子。

->这里是测试<——