清理一些荒谬的JavaScript代码

时间:2016-08-20 11:04:37

标签: javascript jquery

我有三个HTML div的JavaScript,mmsspp。这三个字段相互动画...如果外部文件的内容发生更改,这些字段将在我的页面中更新。他们会更新动画。

如果mm发生变化,则:

  • ss隐藏,然后
  • pp隐藏,然后
  • mm隐藏,然后
  • div更新,然后
  • mm显示,然后
  • pp显示,然后
  • ss显示,然后

如果mm没有变化,但pp没变,那么:

  • ss隐藏,然后
  • pp隐藏,然后
  • div更新,然后
  • pp显示,然后
  • ss显示,然后

如果mmpp没有改变,但ss没改变,那么:

  • ss隐藏,然后
  • div更新,然后
  • ss显示,然后

我运行了这段代码,但它非常麻烦,我想知道是否有更好的方法来做我正在做的事情:

if ($('#mm').html() != mm) {
    hideElem('.score');
    setTimeout(function() {
        hideElem('.player');
        setTimeout(function() {
            hideElem('.match');
            setTimeout(function() {
                updateElems();
                setTimeout(function() {
                    showElem('.match');
                    setTimeout(function() {
                        showElem('.player');
                        setTimeout(function() {
                            showElem('.score');
                        }, inSpeed);
                    }, inSpeed);
                }, outSpeed);
            }, outSpeed);
        }, outSpeed);
    }, outSpeed);
} else if ($('#pp').html() != pp) {
    hideElem('.score');
    setTimeout(function() {
        hideElem('.player');
        setTimeout(function() {
            updateElems();
            setTimeout(function() {
                showElem('.player');
                setTimeout(function() {
                    showElem('.score');
                }, inSpeed);
            }, outSpeed);
        }, outSpeed);
    }, outSpeed);
} else if ($('#ss').html() != ss) {
    hideElem('.score');
    setTimeout(function() {
        updateElems();
        setTimeout(function() {
            showElem('.score');
        }, outSpeed);
    }, outSpeed);
}

setTimeouts的原因是因为所有动画。

1 个答案:

答案 0 :(得分:5)

使用dkempnerPromises

您的代码可能如下所示:

Unresolved error Error Domain=NSCocoaErrorDomain Code=134020 "The model configuration used to open the store is incompatible with the one that was used to create the store." UserInfo={problemObject=<Order: 0x1702a5a00> (entity: Order; id: 0x170431980 <x-coredata:///Order/t38B0CF1D-437E-44AE-B833-D10B5AD584535> ; data: {
    collection = "0xd000000000040004 <x-coredata://D29044CF-B9FB-4D26-9FB3-96FFEE919BD0/Collection/p1>";
    displayOrder = 9999;
    filterUser = "0xd000000000100008 <x-coredata://22E973D7-62F3-4F8F-B85B-CF12DFB8EFD7/FilterUser/p4>";
    quantity = 1;
    size = 1;
}), NSUnderlyingException=Can't resolve how to assign objects to stores; some objects may have been assigned to stores; use [[managedObject objectID] persistentStore] to find out what is going where now; use [managedObjectContext assignObject:toStore:] to straighten things out}, [AnyHashable("problemObject"): <Order: 0x1702a5a00> (entity: Order; id: 0x170431980 <x-coredata:///Order/t38B0CF1D-437E-44AE-B833-D10B5AD5845
Edge旁边的任何浏览器(以及带标志的Chrome)都不支持

async / await,因此您必须使用async/await来编译代码。