Javascript 三元运算符条件永远不会为真,正在尝试多行ajax请求
我正在制作老虎机。三元运算符始终为false,因此ajax请求Javascript 三元运算符条件永远不会为真,正在尝试多行ajax请求,javascript,ajax,ternary-operator,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]
$.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);
/*
requestAnimationFrame多边形填充
*/
(功能(w){
var lastTime=0,
供应商=['webkit'、/*'moz'、*/'o'、'ms'];
对于(var i=0;i')+''+reels[i]。加入('')+''
});
$msg=$('.msg');
$(“按钮”)。单击(操作);
}
函数动作(){
如果(开始!==未定义)返回;
对于(变量i=0;i<3;++i){
速度[i]=Math.random()+.5;
r[i]=(Math.random()*3 | 0)*高度/3;
}
$msg.html('Spinning…');
制作动画();
}
函数动画(现在){
如果(!start)start=now;
var t=now-start | | 0;
对于(变量i=0;i<3;++i)
$revels[i].scrollTop=(速度[i]/tMax/2*(tMax-t)*(tMax-t)+r[i])%高度| 0;
if(t
试试这个:
函数检查(){
$msg.html(r[0]==r[1]&&r[1]==r[2]?
(“你赢了!享受你的”+卷轴[1][(r[0]/70+1)%3 | 0]。拆分(“”)[0],
jqxhr=$.ajax(“addpoint.php”)、console.log(“Enters”))
:
(“再试一次”,
jqxhr=$.ajax(“delpoint.php”))
);
}
它对我有效,我发现的唯一问题是抛出异常
SyntaxError:missing:在条件表达式var jqxhr=$.ajax(“addpoint.php”)
这意味着要在三元运算符中拆分两句话
-->这里有测试试试这个:
函数检查(){
$msg.html(r[0]==r[1]&&r[1]==r[2]?
(“你赢了!享受你的”+卷轴[1][(r[0]/70+1)%3 | 0]。拆分(“”)[0],
jqxhr=$.ajax(“addpoint.php”)、console.log(“Enters”))
:
(“再试一次”,
jqxhr=$.ajax(“delpoint.php”))
);
}
它对我有效,我发现的唯一问题是抛出异常
SyntaxError:missing:在条件表达式var jqxhr=$.ajax(“addpoint.php”)
这意味着要在三元运算符中拆分两句话
-->这里有一个测试语法错误:缺失:在条件表达式var jqxhr=$.ajax(“addpoint.php”)Sry中,我不能说一口流利的英语,我不明白你的意思是什么?你能更简单地解释一下吗?当我运行你的代码时是控制台输出。那么我能做些什么,让代码工作呢?语法错误:缺失:在条件表达式var jqxhr=$.ajax(“addpoint.php”)Sry中,我不能说一口流利的英语,我不明白你的意思是什么?你能更简单地解释一下吗?当我运行你的代码时,控制台是输出的。那么我能做些什么,让代码正常工作呢?