Javascript score target alert

New to JS, please be beautiful.

When creating a Javascript score for playing in a browser canvas, the code below is incremented by 1 for every second. If the value of the variable is 100, how would I go around this function by displaying a window warning when it reaches this value?

Attempts similar to if (score == 100); alert (rating) did not work for me.

Below code will not work in JSFiddle, the output will be displayed on the browser tab.

var start = new Date().getTime(),
    score = '0.1';

window.setInterval(function() {
    var time = new Date().getTime() - start;
    score = Math.floor(time / 1000) ;
    if(Math.round(score) == score) 
        { score += '.0 Score'; }
    document.title = score;
}, 100);
Run codeHide result
+4
source share
1 answer

, . , 100 ( )

- :

var start = new Date().getTime(),
  score = '0.1';

// get handle to interval function
var interval = window.setInterval(function() {
  var time = new Date().getTime() - start;
  
  score = Math.floor(time / 1000);
  console.log(score);
  
  if (score >= 5) { // set to 5 for speedier test/check
    score += '.0 Score';
    window.clearInterval(interval); // clear interval to stop checking 
    alert(score);
  }
  document.getElementById('title').innerHTML = score; // display it
  document.title = score;
}, 100);
<div id="title"></div>
Hide result
+1

Source: https://habr.com/ru/post/1656427/


All Articles