简体   繁体   English

这个javascript闪烁代码的任何原因都不起作用?

[英]Any reason why this javascript blink code won't work?

I have the below code in the second of two js files from a web-app. 我在网络应用程序的两个js文件中的第二个中有以下代码。

It works fine, until I combine the two js files into one. 它工作正常,直到我将两个js文件合并为一个。 Then the js breaks. 然后js打破了。

function oBlink()
{ 
return window.setInterval
( 
    function()
    {
        $("#sOr").css("background-color", function (){ this.switch = !this.switch; return this.switch ? "#F90" : ""  });    
    }
    , 500 
);
}

I've isolated the problem to the code 我已将问题与代码隔离开来了

this.switch = !this.switch; return this.switch ? "#F90" : ""

If I take that out, the rest of my js works fine. 如果我把它拿出来,其余的js工作正常。

I understand there are a lot of external variables that could be coming into play here, but I just wanted to check with you guys that the above function code doesn't have any errors in it. 我知道有很多外部变量可以在这里发挥作用,但我只是想和你们一起检查上面的功能代码中没有任何错误。

Thanks for taking a look. 谢谢参观。

it's working fine in the browser, but failing when checking it on certain devices in the Android emulator. 它在浏览器中工作正常,但在Android模拟器中的某些设备上检查时失败了。

That's probably because you are using switch in your code which is a reserved word in JavaScript. 这可能是因为您在代码中使用了switch ,这是JavaScript中的保留字。 Only ECMAScript5-based browsers allow using reserved words as object's properties. 只有基于ECMAScript5的浏览器允许使用保留字作为对象的属性。

Instead of using a flag you can declare a CSS class and use the jQuery's toggleClass method. 您可以声明CSS类并使用jQuery的toggleClass方法,而不是使用标志。

Make sure you define somewhere 确保你定义在某个地方

switch = false

Then Try 然后试试

$("#sOr").css("background-color", function (){ this.switch = !this.switch; return (this.switch ? "#F90" : "#FFF" ) }); $(“#sOr”)。css(“background-color”,function(){this.switch =!this.switch; return(this.switch?“#F90”:“#FFF”)});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM