jQuery Decremental Loop

时间:2017-08-09 03:46:10

标签: javascript jquery html loops decrement

我需要创建一个jQuery增量/减量循环。

所以我在这里发布了vanilla JavaScript,请有人将此更改为jQuery代码。



var incomeTicker = 60;

window.setInterval(function(){
	
 	if (incomeTicker > 0){
	 	incomeTicker--;
		document.getElementById("incomeTicker").innerHTML = "Next Profit In : " + incomeTicker + " seconds";
// other code implemented as long as incomTicker > 0

	}
	
	if (incomeTicker <= 1){
  
  //code that is implemented when incomeTicker <=1
	
  incomeTicker = 60;
		
  //code that is implemented when incomeTicker <=1
	}
}, 1000);
&#13;
<span class = "incomeTicker" id = "incomeTicker" > Next Profit In : 100 seconds</span>
&#13;
&#13;
&#13;

任何人都可以帮助将其翻译成jQuery吗?它必须递减,然后在循环完成后重置,如代码段

所示

3 个答案:

答案 0 :(得分:2)

您可以使用解决方案https://jsfiddle.net/vh0obhz6/

&#13;
&#13;
var incomeTicker = 60;

window.setInterval(function(){
   if (incomeTicker > 1){
      incomeTicker--;
      $("#incomeTicker").html(`Next Profit In : ${incomeTicker} seconds`);
   }else{
      incomeTicker = 60;
   }
}, 1000);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class = "incomeTicker" id = "incomeTicker" > Next Profit In : 100 seconds</span>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

你应该使用更多的jQuery,它会做所有事情。

以下是您如何添加更多jQuery

&#13;
&#13;
$.incomeTicker = 60;

(function rec() {
  $.each((new Array($.incomeTicker)).fill(0), function(i) {
    var sec = Math.abs(i - $.incomeTicker);
    $('#incomeTicker').delay(1000).queue(function(n) {
      $(this).html("Next Profit In : " + sec + " seconds"); 
      n(); if (sec === 1) rec();
    });
  });
})();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="incomeTicker"></div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

var incomeTicker = 60;

window.setInterval(function(){

    if (incomeTicker > 0){
        incomeTicker--;
        $("#incomeTicker").html("Next Profit In : " + incomeTicker + " seconds");
// other code implemented as long as incomTicker > 0

    }

    if (incomeTicker <= 1){

  //code that is implemented when incomeTicker <=1

  incomeTicker = 60;

  //code that is implemented when incomeTicker <=1
    }
}, 1000);