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 3947536
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:15:47+00:00 2026-05-20T01:15:47+00:00

I am currently working on writing small game that is written in javascript, alot

  • 0

I am currently working on writing small game that is written in javascript, alot like “snake”. Part of it is that me showing grid and that grid needs to be updated often.

And one way for me to do that is using below function.

function loadImages(){

    for(var i = 0;i<HEIGHT;i++){
        for(var j = 0;j<WIDTH;j++){
            if(grid[i][j] == -1){
                document.write('<IMG SRC="wall.png" width="30">');
            }
            else if(grid[i][j] == 0){
                document.write('<IMG SRC="empty.png" width="30">');
            }
        }
    }

    setTimeout( loadImages(), 50 );
}

Only problem is that when loadImages() function is called over and over, images are concatenated not, replacing.

So my questions is that is there a way to overwrite document, or clear?

P.S. “document.clear()” function doesn’t seems to be all that helpful.

Thank you

  • 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-20T01:15:48+00:00Added an answer on May 20, 2026 at 1:15 am

    You could extend your grid data structure and store both the state and the image element:

    // initialization
    for(var i = 0;i<HEIGHT;i++) {
        for(var j = 0;j<WIDTH;j++){
            grid[i][j] = {
                state: grid[i][j],
                image: new Image()   // creates HTMLImageElement object
            };
            // set width
            grid[i][j].image.setAttribute("width", "30");
            // append HTMLImageElement to BODY element
            document.body.appendChild(grid[i][j].image);
        }
    }
    

    Then you can change the images inside loadImages in a similar manner:

    function loadImages() {
        for(var i = 0;i<HEIGHT;i++){
            for(var j = 0;j<WIDTH;j++){
                if(grid[i][j].state == -1){
                    grid[i][j].image.src = "wall.png";
                }
                else if(grid[i][j].state == 0){
                    grid[i][j].image.src = "empty.png";
                }
            }
        }
        setTimeout(loadImages, 50);
    }
    

    By the way: setTimeout(loadImages(), 50) calls loadImages immediately but you should pass the function instead, so setTimeout(loadImages, 50).

    You can optimize this your loadImages function by storing grid[i][j] in a temporary variable and only change the images’ source if the state has changed:

    var cell = grid[i][j];
    switch (cell.state) {
    case -1:
        if (cell.image.src !== "wall.png") {
            cell.image.src = "wall.png";
        }
        break;
    case 0:
        if (cell.image.src !== "empty.png") {
            cell.image.src = "empty.png";
        }
        break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im currently working on a game that uses multi touch to apply zoom to
I'm currently working on a C++ project that needs to have as few external
Currently I'm working on a small program that reads large files and sorts them.
I am currently programming a game that requires reading and writing lines in a
I'm currently building a small web application that includes a fair amount of JavaScript.
I'm currently working on a small utility program that only requires a command line
I'm writing a small Lisp interpreter in C#, and it's basically working already. Currently
I am currently working on a tool and writing it in MVC SPA (single
I am writing an app for my company and am currently working on the
Currently working with converting SQLException error messages into messages that are more useful for

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.