admin管理员组

文章数量:1410705

I've got this function to reset the game after the user presses delete and then presses ok again.

function confirmReset() {
  swal({   
          title: "Are you sure you want to reset your game?",   text: "You will not be able to recover your game!",
           type: "warning",   
           showCancelButton: true,   
           confirmButtonColor: "#DD6B55",   
           confirmButtonText: "Yes, delete it!",   
           closeOnConfirm: false 
         }, function(){   
          swal("Deleted!", "Your imaginary file has been deleted.", "success"), 
            function(){
              window.location.href = "includes/php/reset.php?naam=<?php echo $naam ?>";
            }
      });
}

it works if i do the window.location.href at the place were the second swal('deleted'... etc) is at , but if i use a second function after the user preses OK aswell it wont fire the window.location.href

I've got this function to reset the game after the user presses delete and then presses ok again.

function confirmReset() {
  swal({   
          title: "Are you sure you want to reset your game?",   text: "You will not be able to recover your game!",
           type: "warning",   
           showCancelButton: true,   
           confirmButtonColor: "#DD6B55",   
           confirmButtonText: "Yes, delete it!",   
           closeOnConfirm: false 
         }, function(){   
          swal("Deleted!", "Your imaginary file has been deleted.", "success"), 
            function(){
              window.location.href = "includes/php/reset.php?naam=<?php echo $naam ?>";
            }
      });
}

it works if i do the window.location.href at the place were the second swal('deleted'... etc) is at , but if i use a second function after the user preses OK aswell it wont fire the window.location.href

Share Improve this question asked Jun 20, 2016 at 11:16 StabDevStabDev 7413 gold badges7 silver badges17 bronze badges 4
  • Why not just use setTimeout and refresh page inside that to give time for second alert to show? – charlietfl Commented Jun 20, 2016 at 11:36
  • I tried that, it works. but it isn't pretty. If the user clicks the ok button too early or too late it looks messy. I just want the page to refresh / go to the other page when the user clicks on the second OK @charlietfl – StabDev Commented Jun 20, 2016 at 11:40
  • Isn't there something like onConfirm window.location.hrefetcetcetc ? @charlietfl – StabDev Commented Jun 20, 2016 at 11:44
  • Create a demo. Not clear why what you have wouldn't work – charlietfl Commented Jun 20, 2016 at 11:45
Add a ment  | 

2 Answers 2

Reset to default 2

Try this code :-

function confirmReset() {
  swal({   
          title: "Are you sure you want to reset your game?",   text: "You will not be able to recover your game!",
           type: "warning",   
           showCancelButton: true,   
           confirmButtonColor: "#DD6B55",   
           confirmButtonText: "Yes, delete it!",   
           closeOnConfirm: false 
         }, function(){   
            swal({
                title: "Deleted!",
                text: "Your imaginary file has been deleted.",
                type: "success",
                //timer: 3000
            }, 
            function(){
              window.location.href = "/";
            })
      });
}

By adding a conditional statement & .then, you can redirect on the "OK" Button, and no need to implement the timer. I use this particular method for API calls.

function confirmReset() {
      swal({   
              title: "Are you sure you want to reset your game?",   
              text: "You will not be able to recover your game!",
              type: "warning",   
              showCancelButton: true,   
              confirmButtonColor: "#DD6B55",   
              confirmButtonText: "Yes, delete it!",   
              closeOnConfirm: false 
           }).then((result) => {
                if (result.isConfirmed) {
                 Swal.fire(
                  'Deleted!',
                  'Continue to .....',
                  'success'
                 ).then(function() {
                window.location = "/";
               })      
        }
    }

本文标签: javascriptSweetAlert confirm redirectStack Overflow