javascript - 三元运算符条件永远不为真,尝试多行ajax请求

标签 javascript ajax ternary-operator

我正在创建一台老虎机。三元运算符始终为 false,因此 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"))
  );
}

它对我有用,我发现的唯一问题是抛出异常 语法错误:条件表达式中缺少:var jqxhr = $.ajax(“addpoint.php”)

这意味着你是not using comma opeartor在三元运算符中分割两个句子。

--> Here you have the test <--

关于javascript - 三元运算符条件永远不为真,尝试多行ajax请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38936785/

相关文章:

javascript - Jquery限制输入长度为n封电子邮件

javascript - 谷歌地图加载ajax

javascript - react 三元运算符错误

java - Java 中的 Class clazz 和 Class<?> clazz 有什么区别?

javascript - 通过预渲染 HTML 构建 Marionette View 的简洁方法是什么?

javascript - Angular JS 参数未定义

javascript - 表情符号输入正则表达式搞砸了

javascript - ASP.NET WebMethod 将整个页面返回给 JQuery ajax 请求

javascript - 在绘制图表之前在每个循环中编排嵌套的 AJAX 调用 (JQuery)

javascript - 在 Javascript 中使用三元运算符内联运行 2 个函数