我如何使用 .promise() 才能在我的函数中使用 $.when().done()?

How do I use .promise() so I can work with $.when().done() in my functions?

我有一个 table,它通过我附加到 table 的 JSON 对象获取数据。

我还有一个 select 选项菜单,我想用它来过滤带有所选字符的 table - 类,但应该完成 hardcoreIncursion() 功能过滤前。

我正在尝试为此使用 $.when().done(),但还没有弄清楚我应该如何(或在哪里)使用 .promise() 方法 - jQuery API 文档并没有真正解释它(对我来说)来理解这个概念。

我的伪代码基本上是

when hardcoreIncursion() is .done() then { filter table and only list the chosen character-classes }

我的真实代码是这样的:

<!DOCTYPE html>
<html>
<style>
    body,
    html {
        width: 99, 9%;
    }

    tr,
    td,
    th {
        border: 1px solid black;
        padding: 10px;
    }

    button {
        margin: 20px 0px 20px 0px;
    }

    #dead {
        color: red;
    }

    .online {
        background: green;
    }

    .offline {
        background: red;
    }
</style>

<head>
    <meta charset="utf-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {

            function hardcoreIncursion() {
                $.getJSON("http://api.pathofexile.com/ladders/Hardcore%20Incursion?limit=200&callback=?", function (result) {

                    $.each(result["entries"], function (index, value) {

                        if (value.dead === true) {
                            $("#tdRanking").append(
                                "<tr>"
                                + "<td>" + value.rank + "</td>"
                                + "<td>" + value.account.name + "</td>"
                                + "<td id=\"dead\">" + value.character.name + "</td>"
                                + "<td>" + value.character.level + "</td>"
                                + "<td>" + value.character.experience + "</td>"
                                + "<td>" + value.character.class + "</td>"
                                + "<td>" + value.account.challenges.total + "</td>"
                                + "<td class=\"offline\">" + "</td>"
                                + "</tr>" + "<br/>");
                        }

                        else if (value.online === true) {
                            $("#tdRanking").append(
                                "<tr>"
                                + "<td>" + value.rank + "</td>"
                                + "<td>" + value.account.name + "</td>"
                                + "<td>" + value.character.name + "</td>"
                                + "<td>" + value.character.level + "</td>"
                                + "<td>" + value.character.experience + "</td>"
                                + "<td>" + value.character.class + "</td>"
                                + "<td>" + value.account.challenges.total + "</td>"
                                + "<td class=\"online\">" + "</td>"
                                + "</tr>" + "<br/>");
                        }

                        else {
                            $("#tdRanking").append(
                                "<tr>"
                                + "<td>" + value.rank + "</td>"
                                + "<td>" + value.account.name + "</td>"
                                + "<td>" + value.character.name + "</td>"
                                + "<td>" + value.character.level + "</td>"
                                + "<td>" + value.character.experience + "</td>"
                                + "<td>" + value.character.class + "</td>"
                                + "<td>" + value.account.challenges.total + "</td>"
                                + "<td class=\"offline\">" + "</td>"
                                + "</tr>" + "<br/>");
                        }
                    });
                });

                $("td").remove("td");
                $("tr:empty").remove();

            }

            hardcoreIncursion();

            function Scion() {
                $.when(hardcoreIncursion()).done(function () {
                    $("tr").not("#tableContent").remove(":not(:contains('Scion')):not(:contains('Ascendant'))");
                });
            }

            function Marauder() {
                $.when(hardcoreIncursion()).done(function () {
                    $("tr").not("#tableContent").remove(":not(:contains('Marauder')):not(:contains('Juggernaut')):not(:contains('Berserker')):not(:contains('Chieftain'))");
                });
            }

            function Ranger() {
                $.when(hardcoreIncursion()).done(function () {
                    $("tr").not("#tableContent").remove(":not(:contains('Ranger')):not(:contains('Deadeye')):not(:contains('Raider')):not(:contains('Pathfinder'))");
                });
            }

            function Witch() {
                $.when(hardcoreIncursion()).done(function () {
                    $("tr").not("#tableContent").remove(":not(:contains('Witch')):not(:contains('Necromancer')):not(:contains('Elementalist')):not(:contains('Occultist'))");
                });
            }

            function Duelist() {
                $.when(hardcoreIncursion()).done(function () {
                    $("tr").not("#tableContent").remove(":not(:contains('Duelist')):not(:contains('Slayer')):not(:contains('Gladiator')):not(:contains('Champion'))");
                });
            }

            function Templar() {
                $.when(hardcoreIncursion()).done(function () {
                    $("tr").not("#tableContent").remove(":not(:contains('Templar')):not(:contains('Inquisitor')):not(:contains('Hierophant')):not(:contains('Guardian'))");
                });
            }

            function Shadow() {
                $.when(hardcoreIncursion()).done(function () {
                    $("tr").not("#tableContent").remove(":not(:contains('Shadow')):not(:contains('Trickster')):not(:contains('Assassin')):not(:contains('Saboteur'))");
                });
            }

            const optionFns = {
                0: () => hardcoreIncursion(),
                1: () => Scion(),
                2: () => Marauder(),
                3: () => Ranger(),
                4: () => Witch(),
                5: () => Duelist(),
                6: () => Templar(),
                7: () => Shadow(),
            }

            $("#classFilter").change(function () {
                const value = $('#classFilter option:selected').val();
                optionFns[value]();
            });
        });

    </script>
</head>

<body>

    <div class="controls">
        <select id="classFilter">
            <option value="0" selected>All</option>
            <option value="1">Scion</option>
            <option value="2">Marauder</option>
            <option value="3">Ranger</option>
            <option value="4">Witch</option>
            <option value="5">Duelist</option>
            <option value="6">Templar</option>
            <option value="7">Shadow</option>
        </select>
    </div>

    <div>
        <table>
            <thead>
                <tr id="tableContent">
                    <th>Rang</th>
                    <th>Accountname</th>
                    <th>Charaktername</th>
                    <th>Level</th>
                    <th>Erfahrung</th>
                    <th>Klasse</th>
                    <th>Herausforderungen</th>
                    <th>Online</th>
                </tr>
            </thead>
            <tbody id="tdRanking">
            </tbody>
        </table>
    </div>

</body>

</html>

提前致谢

以下代码将解决您的问题。

 <!DOCTYPE html>
 <html>
 <style>
   body,
   html {
     width: 99, 9%;
   }

   tr,
   td,
   th {
     border: 1px solid black;
     padding: 10px;
   }

   button {
     margin: 20px 0px 20px 0px;
   }

   #dead {
     color: red;
   }

   .online {
     background: green;
   }

   .offline {
     background: red;
   }
 </style>

 <head>
   <meta charset="utf-8">
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
   <script>
     $(document).ready(function () {
       var hardcoreIncursion = $.getJSON("http://api.pathofexile.com/ladders/Hardcore%20Incursion?limit=200&callback=?", callbackFun);

       $("td").remove("td");
       $("tr:empty").remove();

       function callbackFun(result) {

         $.each(result["entries"], function (index, value) {

           if (value.dead === true) {
             $("#tdRanking").append(
               "<tr>"
               + "<td>" + value.rank + "</td>"
               + "<td>" + value.account.name + "</td>"
               + "<td id=\"dead\">" + value.character.name + "</td>"
               + "<td>" + value.character.level + "</td>"
               + "<td>" + value.character.experience + "</td>"
               + "<td>" + value.character.class + "</td>"
               + "<td>" + value.account.challenges.total + "</td>"
               + "<td class=\"offline\">" + "</td>"
               + "</tr>" + "<br/>");
           }

           else if (value.online === true) {
             $("#tdRanking").append(
               "<tr>"
               + "<td>" + value.rank + "</td>"
               + "<td>" + value.account.name + "</td>"
               + "<td>" + value.character.name + "</td>"
               + "<td>" + value.character.level + "</td>"
               + "<td>" + value.character.experience + "</td>"
               + "<td>" + value.character.class + "</td>"
               + "<td>" + value.account.challenges.total + "</td>"
               + "<td class=\"online\">" + "</td>"
               + "</tr>" + "<br/>");
           }

           else {
             $("#tdRanking").append(
               "<tr>"
               + "<td>" + value.rank + "</td>"
               + "<td>" + value.account.name + "</td>"
               + "<td>" + value.character.name + "</td>"
               + "<td>" + value.character.level + "</td>"
               + "<td>" + value.character.experience + "</td>"
               + "<td>" + value.character.class + "</td>"
               + "<td>" + value.account.challenges.total + "</td>"
               + "<td class=\"offline\">" + "</td>"
               + "</tr>" + "<br/>");
           }
         });
       };



       function Scion() {
         hardcoreIncursion.done(function (result) {
           callbackFun(result);
           $("tr").not("#tableContent").remove(":not(:contains('Scion')):not(:contains('Ascendant'))");
         });
       }

       function Marauder() {
         hardcoreIncursion.done(function (result) {
           callbackFun(result);
           $("tr").not("#tableContent").remove(":not(:contains('Marauder')):not(:contains('Juggernaut')):not(:contains('Berserker')):not(:contains('Chieftain'))");
         });
       }

       function Ranger() {
         hardcoreIncursion.done(function (result) {
           callbackFun(result);
           $("tr").not("#tableContent").remove(":not(:contains('Ranger')):not(:contains('Deadeye')):not(:contains('Raider')):not(:contains('Pathfinder'))");
         });
       }

       function Witch() {
         hardcoreIncursion.done(function (result) {
           callbackFun(result);
           $("tr").not("#tableContent").remove(":not(:contains('Witch')):not(:contains('Necromancer')):not(:contains('Elementalist')):not(:contains('Occultist'))");
         });
       }

       function Duelist() {
         hardcoreIncursion.done(function (result) {
           callbackFun(result);
           $("tr").not("#tableContent").remove(":not(:contains('Duelist')):not(:contains('Slayer')):not(:contains('Gladiator')):not(:contains('Champion'))");
         });
       }

       function Templar() {
         hardcoreIncursion.done(function (result) {
           callbackFun(result);
           $("tr").not("#tableContent").remove(":not(:contains('Templar')):not(:contains('Inquisitor')):not(:contains('Hierophant')):not(:contains('Guardian'))");
         });
       }

       function Shadow() {
         hardcoreIncursion.done(function (result) {
           callbackFun(result);
           $("tr").not("#tableContent").remove(":not(:contains('Shadow')):not(:contains('Trickster')):not(:contains('Assassin')):not(:contains('Saboteur'))");
         });
       }

       const optionFns = {
         0: () => hardcoreIncursion.done(function (result) {
           callbackFun(result);
         }),
         1: () => Scion(),
         2: () => Marauder(),
         3: () => Ranger(),
         4: () => Witch(),
         5: () => Duelist(),
         6: () => Templar(),
         7: () => Shadow(),
       }

       $("#classFilter").change(function () {
         const value = $('#classFilter option:selected').val();
         optionFns[value]();
       });
     });

   </script>
 </head>

 <body>

   <div class="controls">
     <select id="classFilter">
       <option value="0" selected>All</option>
       <option value="1">Scion</option>
       <option value="2">Marauder</option>
       <option value="3">Ranger</option>
       <option value="4">Witch</option>
       <option value="5">Duelist</option>
       <option value="6">Templar</option>
       <option value="7">Shadow</option>
     </select>
   </div>

   <div>
     <table>
       <thead>
         <tr id="tableContent">
           <th>Rang</th>
           <th>Accountname</th>
           <th>Charaktername</th>
           <th>Level</th>
           <th>Erfahrung</th>
           <th>Klasse</th>
           <th>Herausforderungen</th>
           <th>Online</th>
         </tr>
       </thead>
       <tbody id="tdRanking">
       </tbody>
     </table>
   </div>

 </body>

 </html>