简体   繁体   English

单击停止按钮后如何停止执行JavaScript?

[英]How to stop javascript from executing after clicking stop button?

    <script>
function timer(duration, elementId, scoreID, mistakesID){
  var start = Date.now();
  var min, sec, diff;
  var scoreComputed = false;

  function timerCompute(){
    diff = duration - (((Date.now() - start) / 1000) | 0);

    min = ( diff / 60 ) | 0;
    sec = ( diff % 60 ) | 0;

    //If less than 10, add zero in front of it to keep tens place
    min = min < 10 ? "0" + min : min;
    sec = sec < 10 ? "0" + sec : sec;

    elementId.textContent = min + ":" + sec;

    if ( diff <= 0 ){
      elementId.textContent = "0:00";

      if (!scoreComputed){
        score /= (duration / 60);
        score = Math.round(score);
        scoreComputed = true;
      }

      scoreID.textContent = score;
      document.getElementById("usertext").readOnly = true;

      $("#resultsContainer").fadeIn(1500);

      if(mistakes.length > 0){
        var mistakesStr = "";

        for(var i = 0; i < mistakes.length; i++){
          mistakesStr += i > 0 ? ", " : "";
          usertextArr[mistakes[i]] = usertextArr[mistakes[i]] === "" ? "blank" : usertextArr[mistakes[i]];
          mistakesStr += usertextArr[mistakes[i]] + " instead of " + textArr[mistakes[i]];;
        }
        mistakesID.textContent = mistakesStr;
        $("#mistakesContainer").fadeIn(1500);
         $('.type-test-wrapper').css("filter","blur(3px)");
      }
      return;
    }
  };
  timerCompute();
  setInterval(timerCompute, 1000);
}

var timerStarted = false;
$('#usertext').on('keydown', function() {

  var time = 60 * 0.1,
  display = document.querySelector('#time');
  scoreDisplay = document.querySelector('#score');
  mistakesDisplay = document.querySelector('#mistakes');
  if ( timerStarted === false ){
    timer(time, display, scoreDisplay, mistakesDisplay);
    typeTest();
    timerStarted = true;
  }
});
</script>

The above script is from a typing test. 上面的脚本来自打字测试。 When the typing test ends, the above script fades in a result box. 当键入测试结束时,上述脚本会在结果框中消失。 I was trying to modify the code. 我正在尝试修改代码。 I want to add a close button to the result box so that user can close the box after he viewed the result. 我想在结果框中添加一个关闭按钮,以便用户可以在查看结果后关闭该框。 So I added the below code to close the result box. 因此,我添加了以下代码以关闭结果框。

<script type='text/javascript'>
jQuery(document).ready(function($){
$('.close-btn, body').click(function(){
$('.type-test-wrapper').css("filter","blur(0px)");
$('#resultsContainer, #mistakesContainer').stop().fadeOut('medium');
});

});
</script>

The problem I am facing is that when I click on close button, the box fades out #resultContainer, #mistakesContainer but as soon the box gets faded, it again fades in. No matter how many times i close the box, it fades in again and again. 我面临的问题是,当我单击关闭按钮时,该框会淡出#resultContainer, #mistakesContainer但是一旦该框消失,它就会再次淡入。无论我关闭该框多少次,它都会再次淡入然后再次。 I want that once user click on close button the box should not get displayed again. 我希望用户单击关闭按钮后,该框不应再次显示。 How to fix the code so that once the the box fades out, the first code doesn't again fades in the box. 如何修复代码,以便一旦盒子淡出,第一个代码就不会再次在盒子中淡出。 I am new at javascript. 我是javascript新手。 Thank you for help. 谢谢你的帮助。

Live webpage here https://htmlpreview.github.io/?https://raw.githubusercontent.com/dp121112/type-testing/master/test.html 实时网页在这里https://htmlpreview.github.io/?https://raw.githubusercontent.com/dp121112/type-testing/master/test.html

I think you're not clearing the interval try to assing interval as follow. 我认为您没有清除间隔尝试按如下所述间隔时间。

var intrvl= setInterval(timerCompute, 1000); 

and clear it when its done 并在完成后清除它

scoreID.textContent = score;
document.getElementById("usertext").readOnly = true;
$("#resultsContainer").fadeIn(1500);
clearInterval(intrvl);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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