0

I'm implementing the extra feature for pset8 where the user has only a limited amount of time to play the game before it ends.

My aim is to load the window and Google Earth, popup an alert stating the rules, start the game, and then load another popup when the timer runs out. I decided to do this in the window.load() function:

$(window).load(function() {

    // listen for keydown anywhere in body
    $(document.body).keydown(function(event) {
        return keystroke(event, true);
    });

    // listen for keyup anywhere in body
    $(document.body).keyup(function(event) {
        return keystroke(event, false);
    });

    // listen for click on Drop Off button
    $("#dropoff").click(function(event) {
        dropoff();
    });

    // listen for click on Pick Up button
    $("#pickup").click(function(event) {
        pickup();
    });

    //load application
    load();

    //alert player of game rules
    alert("Sample Game Rules");

});

As you can see, at the end, AFTER the load() function is called, I alert the user of the rules. However, this alert comes up BEFORE load() has loaded up the map. When I put in

window.setTimeout(end_game(), 10000);

I have the same problem. Instead of waiting until Earth has loaded, the alert displayed by end_game() is immediately displayed, without even a 10 second delay as is expected.

What am I doing wrong? Have I misunderstood alert()?

1 Answer 1

0

This is precisely what the jquery ready event handler was built for, documentation here:http://api.jquery.com/

3
  • This isn't really what I need. According to the documentation this function allows you to execute script as soon as the "DOM hierarchy is fully constructed." What I want to do is execute a script after the load() function has been called. I would think this would be as easy as putting the script after the load(), but that hasn't worked.
    – ajtalkar
    Aug 25, 2014 at 22:01
  • w3schools.com/jQuery/jquery_syntax.asp
    – ronga
    Aug 26, 2014 at 2:15
  • Thanks. This was my first time using JavaScript, so I wasn't really sure how this all worked.
    – ajtalkar
    Aug 27, 2014 at 17:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .