我打开一个窗口,想在某些条件下进行一些计算.问题是在IE中,只有第一呼叫test(从ready)是由新窗口中执行,但后续调用test(带的setTimeout设置)在父窗口而不是用于新窗口(即执行win).
它在FF中工作正常.
说明代码(此代码在父窗口中):
var win = window.open(some_url, window_name, argument_string);
with (win) {
function test() {
alreadyrunflag += 1;
if (alreadyrunflag < 10) {
window.setTimeout(function() { test(); }, 500);
}else {
//perform calculation
}
}
jQuery(win.document).ready(function() {
alreadyrunflag = 0
test();
});
}
Run Code Online (Sandbox Code Playgroud) 我认为那样的东西with(Math){document.body.innerHTML= PI}不是很好的做法.
在"JavaScript the Good Parts"中,with被认为是javascript的一个不好的部分,但请看这个片段:
var foo={foof:function(){console.log(this)}}
var fuu={fuuf:function(){console.log(this)}}
with(foo){
console.log(this);
with(fuu){
console.log(this);
foof();
fuuf();
}
}
Run Code Online (Sandbox Code Playgroud)
是否with真的如此糟糕做法?with有时可以提供乐趣或优势,谁可以举个例子?
if (!wysiwyg_toolbarButtons) {
var wysiwyg_toolbarButtons = new Array(
//command, display name, value, title, prompt/function, default text
["bold", "Strong", WYSIWYG_VALUE_NONE, "Give text strength"],
["italic", "Emphasis", WYSIWYG_VALUE_NONE, "Give text emphasis"],
["createlink", "Link", WYSIWYG_VALUE_PROMPT, "Create a hyperlink", "Enter the URL:", "http://"],
["unlink", "Unlink", WYSIWYG_VALUE_NONE, "Remove hyperlink"],
["insertimage", "Image", WYSIWYG_VALUE_PROMPT, "Insert an image", "Enter the URL of the image:", "http://"],
["inserthorizontalrule", "Rule", WYSIWYG_VALUE_NONE, "Insert horizontal rule"],
["div"], // place a toolbar divider
["formatblock", "Headling 1", "<H1>", "Make top level heading"],
["formatblock", "Headling 2", "<H2>", …Run Code Online (Sandbox Code Playgroud) 非常简单的问题:我想优化以下jQuery代码,具有最大的可读性,最佳性能和最小化(fuss =声明新变量等):
$(".addthis_toolbox").append('<a class="addthis_button_delicious"></a>');
$(".addthis_toolbox").append('<a class="addthis_button_facebook"></a>');
$(".addthis_toolbox").append('<a class="addthis_button_google"></a>');
$(".addthis_toolbox").append('<a class="addthis_button_reddit"></a>');
.
.
.
$(".addthis_toolbox").append('<a class="addthis_button_yetanotherservice"></a>');
Run Code Online (Sandbox Code Playgroud) 我正在尝试用HTML/JS编写游戏Simon,但它完全正常工作,除了游戏闪烁序列的部分,所以你知道新的序列是什么.基本上我拥有的是:
for(var i in thePattern){
var obj = document.getElementById(thePattern[i]);
window.setTimeout(colorON(obj),500);
window.setTimeout(colorOFF(obj),1000);
}
Run Code Online (Sandbox Code Playgroud)
其中colorON和colorOFF是:
function colorON(obj){
if(obj.id == "red"){
obj.style.backgroundColor="#ff5555";
}else if(obj.id == "blue"){
obj.style.backgroundColor="#5555ff";
}else if(obj.id == "green"){
obj.style.backgroundColor="#88ff88";
}else{
obj.style.backgroundColor="#ffffaa";
}
}
function colorOFF(obj){
if(obj.id == "red"){
obj.style.backgroundColor="#ff0000";
}else if(obj.id == "blue"){
obj.style.backgroundColor="#0000ff";
}else if(obj.id == "green"){
obj.style.backgroundColor="#22ff22";
}else{
obj.style.backgroundColor="#ffff00";
}
}
Run Code Online (Sandbox Code Playgroud)
它似乎正在做的是通过整个for循环并启动所有计时器,然后所有计时器快速下降,以至于颜色甚至看起来都不闪烁.
有任何想法吗?非常感谢所有帮助.
现在它正确闪烁并且关闭工作正常,但它同时闪烁所有颜色.我试过把闭包放在另一个setTimeout,但是,这只会产生其他问题.
解决感谢您的帮助球员.
来自MDN:
使用
with不推荐,并在ECMAScript中5严格模式是被禁止的.建议的替代方法是将要访问其属性的对象分配给临时变量.
这似乎是一个伟大/有用/方便的功能.为什么不赞成?还有哪些方法可以达到这种效果?我不想去:
veryLongNS.y = veryLongNS.myFunc(veryLongNS.x);
veryLongNS.z = 6;
veryLongNS.otherFunc();
veryLongNS.a = {
a:1,
b:2,
c:veryLongNS.processThree(3)
};
Run Code Online (Sandbox Code Playgroud) javascript ×9
for-loop ×1
jquery ×1
namespaces ×1
object ×1
optimization ×1
scope ×1
settimeout ×1