More elegant way to use database outputs in a search shortlist [javascript/jquery]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I'm just trying to make a search bar that will allow the users to type in a substring of any page on my website, and the search bar will shortlist page names that are consistent with what they've typed.



I'm ajaxing in relevant pages, though I'm wondering whether it's more expensive to do ajaxes than just download a list of every possible page at the beginning, even if it's thousands? The list of pages is stored in a MySQL table. I'm also wondering whether there are other ways to make the code more elegant?



NOTE: I've simplified where the Ajaxing occurs to make it run, everything else is working code I'm using in my project.



My solution:






search_block = false;
$("#search_bar_input").on("keyup",function()
if(search_block == false)
search_block = true;
setTimeout(function() //timeout to block overburdening of ajaxing of databases
search_block = false;
console.dir($("#search_bar_input").val());
var search_term = $("#search_bar_input").val();
// NOTE: this ajax_output below represents what sort of output the ajax is generating.
ajax_output = ["input1", "input2", "input3", "input4"];
$("#search_bar_input" ).autocomplete(
source: ajax_output,
);
$('#search_bar_input').autocomplete("search"); // or else the text in the search input isn't implemented after the timeout completes
, 1000);

);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.theme.min.css">

<form class="form-inline my-2 my-lg-0">
<div>
<input id="search_bar_input" class="form-control mr-sm-2 typeahead" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</div>
</form>









share|improve this question



























    up vote
    0
    down vote

    favorite












    I'm just trying to make a search bar that will allow the users to type in a substring of any page on my website, and the search bar will shortlist page names that are consistent with what they've typed.



    I'm ajaxing in relevant pages, though I'm wondering whether it's more expensive to do ajaxes than just download a list of every possible page at the beginning, even if it's thousands? The list of pages is stored in a MySQL table. I'm also wondering whether there are other ways to make the code more elegant?



    NOTE: I've simplified where the Ajaxing occurs to make it run, everything else is working code I'm using in my project.



    My solution:






    search_block = false;
    $("#search_bar_input").on("keyup",function()
    if(search_block == false)
    search_block = true;
    setTimeout(function() //timeout to block overburdening of ajaxing of databases
    search_block = false;
    console.dir($("#search_bar_input").val());
    var search_term = $("#search_bar_input").val();
    // NOTE: this ajax_output below represents what sort of output the ajax is generating.
    ajax_output = ["input1", "input2", "input3", "input4"];
    $("#search_bar_input" ).autocomplete(
    source: ajax_output,
    );
    $('#search_bar_input').autocomplete("search"); // or else the text in the search input isn't implemented after the timeout completes
    , 1000);

    );

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.theme.min.css">

    <form class="form-inline my-2 my-lg-0">
    <div>
    <input id="search_bar_input" class="form-control mr-sm-2 typeahead" type="search" placeholder="Search" aria-label="Search">
    <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </div>
    </form>









    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm just trying to make a search bar that will allow the users to type in a substring of any page on my website, and the search bar will shortlist page names that are consistent with what they've typed.



      I'm ajaxing in relevant pages, though I'm wondering whether it's more expensive to do ajaxes than just download a list of every possible page at the beginning, even if it's thousands? The list of pages is stored in a MySQL table. I'm also wondering whether there are other ways to make the code more elegant?



      NOTE: I've simplified where the Ajaxing occurs to make it run, everything else is working code I'm using in my project.



      My solution:






      search_block = false;
      $("#search_bar_input").on("keyup",function()
      if(search_block == false)
      search_block = true;
      setTimeout(function() //timeout to block overburdening of ajaxing of databases
      search_block = false;
      console.dir($("#search_bar_input").val());
      var search_term = $("#search_bar_input").val();
      // NOTE: this ajax_output below represents what sort of output the ajax is generating.
      ajax_output = ["input1", "input2", "input3", "input4"];
      $("#search_bar_input" ).autocomplete(
      source: ajax_output,
      );
      $('#search_bar_input').autocomplete("search"); // or else the text in the search input isn't implemented after the timeout completes
      , 1000);

      );

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>

      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.theme.min.css">

      <form class="form-inline my-2 my-lg-0">
      <div>
      <input id="search_bar_input" class="form-control mr-sm-2 typeahead" type="search" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
      </div>
      </form>









      share|improve this question













      I'm just trying to make a search bar that will allow the users to type in a substring of any page on my website, and the search bar will shortlist page names that are consistent with what they've typed.



      I'm ajaxing in relevant pages, though I'm wondering whether it's more expensive to do ajaxes than just download a list of every possible page at the beginning, even if it's thousands? The list of pages is stored in a MySQL table. I'm also wondering whether there are other ways to make the code more elegant?



      NOTE: I've simplified where the Ajaxing occurs to make it run, everything else is working code I'm using in my project.



      My solution:






      search_block = false;
      $("#search_bar_input").on("keyup",function()
      if(search_block == false)
      search_block = true;
      setTimeout(function() //timeout to block overburdening of ajaxing of databases
      search_block = false;
      console.dir($("#search_bar_input").val());
      var search_term = $("#search_bar_input").val();
      // NOTE: this ajax_output below represents what sort of output the ajax is generating.
      ajax_output = ["input1", "input2", "input3", "input4"];
      $("#search_bar_input" ).autocomplete(
      source: ajax_output,
      );
      $('#search_bar_input').autocomplete("search"); // or else the text in the search input isn't implemented after the timeout completes
      , 1000);

      );

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>

      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.theme.min.css">

      <form class="form-inline my-2 my-lg-0">
      <div>
      <input id="search_bar_input" class="form-control mr-sm-2 typeahead" type="search" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
      </div>
      </form>








      search_block = false;
      $("#search_bar_input").on("keyup",function()
      if(search_block == false)
      search_block = true;
      setTimeout(function() //timeout to block overburdening of ajaxing of databases
      search_block = false;
      console.dir($("#search_bar_input").val());
      var search_term = $("#search_bar_input").val();
      // NOTE: this ajax_output below represents what sort of output the ajax is generating.
      ajax_output = ["input1", "input2", "input3", "input4"];
      $("#search_bar_input" ).autocomplete(
      source: ajax_output,
      );
      $('#search_bar_input').autocomplete("search"); // or else the text in the search input isn't implemented after the timeout completes
      , 1000);

      );

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>

      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.theme.min.css">

      <form class="form-inline my-2 my-lg-0">
      <div>
      <input id="search_bar_input" class="form-control mr-sm-2 typeahead" type="search" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
      </div>
      </form>





      search_block = false;
      $("#search_bar_input").on("keyup",function()
      if(search_block == false)
      search_block = true;
      setTimeout(function() //timeout to block overburdening of ajaxing of databases
      search_block = false;
      console.dir($("#search_bar_input").val());
      var search_term = $("#search_bar_input").val();
      // NOTE: this ajax_output below represents what sort of output the ajax is generating.
      ajax_output = ["input1", "input2", "input3", "input4"];
      $("#search_bar_input" ).autocomplete(
      source: ajax_output,
      );
      $('#search_bar_input').autocomplete("search"); // or else the text in the search input isn't implemented after the timeout completes
      , 1000);

      );

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>

      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.theme.min.css">

      <form class="form-inline my-2 my-lg-0">
      <div>
      <input id="search_bar_input" class="form-control mr-sm-2 typeahead" type="search" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
      </div>
      </form>








      share|improve this question












      share|improve this question




      share|improve this question








      edited Mar 23 at 8:32









      Billal BEGUERADJ

      1




      1









      asked Mar 23 at 8:27









      it Haffens

      12




      12

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190276%2fmore-elegant-way-to-use-database-outputs-in-a-search-shortlist-javascript-jquer%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190276%2fmore-elegant-way-to-use-database-outputs-in-a-search-shortlist-javascript-jquer%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Greedy Best First Search implementation in Rust

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          C++11 CLH Lock Implementation