简体   繁体   中英

Refresh and call function in script

So I have a function in a script called start() and at the end of the start() function I will ask if the user if s/he wants to play again. If they do want to play again, i want the page to refresh and im doing this using location.reload() and then i want to start from the beginning of the start function again. However, when i simply do this...

location.reload();
start();

it doesn't refresh the page. I was wondering if there is another way for me to go about doing this?

More Info:

this is a black jack game that i am creating for learning purposes.

start function is called when a button is clicked.

in the start function, the dealer and player cards will be shown and then the player has option to either hit or hold.

I wanted all of the things done in the start function erased on the webpage and i done it by refreshing the page(it was the simplest way to me). and then i wanted to call the start function again when the page is refreshed and the user selects okay on the confirm dialog box that he wants to play again.

**EDIT: http://jsfiddle.net/MAbgW/7/ my code is here. pls help if you're free. seems like an easy fix i think i might just be saying it in a confusing way.

Have you tried...

location.reload(true);

Alternatively, you could do...

location.href = location.href;

This would simply send them back to the same URL.

This should do it. Reloads the page and stops the rest of the start() function, unless the user is done.

EDIT:

function start(){
    alert("Going...");
    var cont = confirm("Do you want to continue?");
    if(cont){ location.reload(); end; } // notice the end

    alert("Let me know if you see this when you select OK");
}
start();

This code will work: http://jsfiddle.net/MAbgW/9/

You don't have to reload page. Restart your function in a loop like this:

function start() {

    do {

        alert('The Game is On!');

    } while (confirm('Do you want to play again?'))

}

start();

Do not use page reload as a means of reinitializing data - rather initialize them at the beginning of your start function (first thing inside of do block in code above)

Demo with data init: http://jsfiddle.net/e9W6q/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM