gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:40:52 25 4
gpt4 key购买 nike

我正在创建一台老虎机。三元运算符始终为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com