如何在ajax页面重新加载时清除js数组

时间:2016-06-26 01:35:13

标签: javascript ajax

我有一个绘制到画布上的js,当发生php会话更改时我会更改颜色值(这可行)。虽然每次重装都会使速度不断提高。我将如何重置js,以便当页面重新加载时,它会触发数组变量的新迭代,并且它不会复合到下一个。

<style>

 body {
    background: black;
    overflow: hidden;
  color: white;
}


  #iso {
  left: 0%;
  top: 0%;
  position: relative;
  height: 100%;
  width: 100%;
  z-index: 3;

  }

  #isohold {
  top: 37.5%;
  left: 37.5%;
  position: absolute;
  height: 100%;
  width: 100%;

  }

  #loghold {
  top: -5%;
  left: 0%;
  position: relative;
  height: 15%;
  width: 100%;
  color: rgba(255, 255, 255, 1.0);
  font-size: 12;
  text-align: center;
  z-index: 5;
  display: visible;
  }
</style>

<div id="isohold">  
<canvas id="iso"></canvas>
<div id="loghold">Login</div>
</div>










  <?php
    session_start();         




    //3.1.4 if the user is logged in Greets the user with message     
    if (isset($_SESSION['userid'])){
    $userid = $_SESSION['userid'];
   $colorin = 'rgba(81, 180, 200, 0.5)';    

    echo "
<script>
                 $(\"#loghold\").hide();   
</script>       
            ";
    }else{ 

      $colorin = 'rgba(255, 255, 255, 0.5)';         



  };

      echo "  "; 



 ?>


<script>

'use strict';


var rn = function rn(min, max) {
    return Math.random() * (max - min) + min;
};
var ctx = iso.getContext('2d');

var _window = window;
var w = _window.innerWidth;
var h = _window.innerHeight;

var t = 10;
var arr = [];
var cn = 200;
var rad = 300;
var sp = rn(1, 5) / 10000;

iso.width = w;
iso.height = h;

while (~ ~ cn--) {
    var angle = rn(110, 359);

    arr = [].concat(arr, [{
        color: '<?php echo $colorin ?>',
        distortion: rn(15, 75),
        tmod: rn(5, 10),
        size: rn(15, 20),
        speed: 0.0005,
        angle: angle,
        lastPos: {
            x: w / 2,
            y: h / 2
        }
    }]);
}

var draw = function draw() {
    request = requestAnimationFrame(function () {
        return draw();
    });
    t++;

    ctx.globalCompositeOperation = 'source-over';
    ctx.fillStyle = 'rgba(0, 0, 0,.1)';
    ctx.fillRect(0, 0, w, h);

    var crad = rad * Math.sin(300);

    ctx.globalCompositeOperation = 'lighter';
    arr.forEach(function (el) {
        ctx.strokeStyle = el.color;
        ctx.lineWidth = el.size;
        ctx.beginPath();

        var lastPos = el.angle - el.speed;
        var x = w / 2 + (crad + el.distortion * Math.sin(t / el.tmod)) * Math.cos(el.angle * 180 / Math.PI);
        var y = h / 2 + (crad + el.distortion * Math.sin(t / el.tmod)) * Math.sin(el.angle * 180 / Math.PI);

        ctx.moveTo(el.lastPos.x, el.lastPos.y);
        ctx.lineTo(x, y);

        el.lastPos = { x: x, y: y };
        el.angle = (el.angle + 0.0005) % 359;
        ctx.stroke();
    });
};

var resize = function resize() {
    iso.width = w = window.innerWidth;
    iso.height = h = window.innerHeight;
};

var request = requestAnimationFrame(function () {
    return draw();
});
window.addEventListener('resize', function () {
    return resize();
});

</script>             

<script>

$(document).ready(function(){
    $("#loghold").click(function(){
        $("#loginholder").show();
        $("#loginholder").load("/halcyon/pages/login/loginscript.php");
        $("#controlremove").show();
    });

   });    
</script>  

ajax加载是一个简单的

$("#isoholder").load("/halcyon/pages/iso/iso.php");

0 个答案:

没有答案