Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4609262
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:57:43+00:00 2026-05-22T00:57:43+00:00

I created a map where a user can walk from one building to another.

  • 0

I created a map where a user can walk from one building to another. Next to the map there’s a small minimap. The “big” map just refreshes by using the load() command. I have got the following code:

    $(document).keydown(function(event) {

        switch (event.keyCode) {

            // A
            case 65: $("#world").load("../modules/Map.php?go&move=w"); break;

            // W
            case 87: $("#world").load("../modules/Map.php?go&move=n"); break;

            // D
            case 68: $("#world").load("../modules/Map.php?go&move=e"); break;

            // S
            case 83: $("#world").load("../modules/Map.php?go&move=s"); break;

        }

    });

Now, right after pressing one of the keys, I want the following to be executed:

$("#minimap").load("../modules/Minimap.php");

I hope you can help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:57:43+00:00Added an answer on May 22, 2026 at 12:57 am

    You have two choices, but first, let’s refactor slightly (I’ll refactor more in a moment, but let’s start with this):

    $(document).keydown(function(event) {
        var direction;
    
        switch (event.keyCode) {
    
            // A
            case 65: direction = 'w'; break;
    
            // W
            case 87: direction = 'n'; break;
    
            // D
            case 68: direction = 'e'; break;
    
            // S
            case 83: direction = 's'; break;
        }
    
        if (direction) {
            $("#world").load("../modules/Map.php?go&move=" + direction);
        }
    });
    

    Now the first load is centralized and we’re not repeating ourselves.

    Okay, you have two choices:

    1. Put it after the switch statement if you want both loads to happen at the same time. E.g., picking up at the end:

      if (direction) {
          $("#world").load("../modules/Map.php?go&move=" + direction);
          $("#minimap").load("../modules/Minimap.php");
      }
      

      I’ve assumed there that we only want to do it if a key matched. If I’m wrong, move it out of the if.

    2. Use the success callback on the first load if you want the second load to wait until the first one is complete.

      if (direction) {
          $("#world").load("../modules/Map.php?go&move=" + direction, function() {
              // This gets called when the load completes
              $("#minimap").load("../modules/Minimap.php");
          });
      }
      

    More in the docs.


    Here’s the more thoroughly refactored version: Have this:

    var directionKeyMap = {
        '65': 'w',  // A
        '87': 'n',  // W
        '68': 'e',  // D
        '83': 's'   // S
    };
    

    And then either this (option 1 above):

    $(document).keydown(function(event) {
        var direction;
    
        direction = directionKeyMap[event.keyCode];
        if (direction) {
            $("#world").load("../modules/Map.php?go&move=" + direction);
            $("#minimap").load("../modules/Minimap.php");
        }
    });
    

    Or this (option 2 above):

    $(document).keydown(function(event) {
        var direction;
    
        direction = directionKeyMap[event.keyCode];
        if (direction) {
            $("#world").load("../modules/Map.php?go&move=" + direction, function() {
                $("#minimap").load("../modules/Minimap.php");
            });
        }
    });
    

    Those use an object to map keycodes to directions, taking advantage of the fact you can look up object properties using bracketed notation.

    (Don’t worry that keyCode is a number but the keys in our map are strings; whenever you use bracketed notation, whatever you give is converted to a string by the JavaScript interpreter. In fact, that’s the case even when you index into an array, since JavaScript arrays aren’t really arrays. But again, we’re using a plain object, not an array.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails app where users can follow one another. I've just discovered
Hi everyone! Im working on a google map project where the user can type
I need to capture user's X.509 certificates from their cards and map to a
I have created a map which allows the user to plot multiple markers with
I use the google map tool from primefaces . I want my user to
I've created a map system for a game that runs on the principle of
Basic question : How to I create a bidirectional one-to-many map in Fluent NHibernate?
I created a program using dev-cpp and wxwidgets which solves a puzzle. The user
I created an Interop user control in VS2005. When the user control is shown
On my site, the user can watch his profile. In his profile he can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.