【问题标题】:Javascript increasing a value and the velocity on increasing itJavascript增加一个值和增加它的速度
【发布时间】:2016-06-19 11:35:25
【问题描述】:

我需要增加一个数字,我使用了setInterval(Function, time) 所以我放了一个时间变量:time = 1000 现在我需要更改它,所以我放了一个函数,当我单击按钮时会更改它:

function changetime() {time = time - 100;}

但您似乎无法在工作时更改setInterval 的时间... 我怎样才能做到这一点? 我尝试使用setTimeout,但现在数字变为“跳跃”。不规律…… 我不确定,但是当我更改setTimeout 时间时,“跳转”似乎发生了变化......就像超时现在在setInterval 时间一样。

原码---_>

var time = 1000;
function interval() { setInterval(Function, time);}
function changetime() {setTimeout(interval, 10);tempo = tempo - 200;}

【问题讨论】:

  • 感谢您的帮助....现在在控制台中我有时间更改....从 1000 更改为 800 但数字仍然跳跃....
  • 这里是您问题的答案:stackoverflow.com/questions/1280263/…
  • 我认为这篇文章只是为了减少时间......我需要加快速度。如果你减少 setInterval 循环的时间,它会更快......
  • 好的,我解决了!最好使用 SetTimeout 执行此操作,然后重新调用它,因为在 settimeout 中可以更改时间......无论如何谢谢......:D

标签: javascript html time settimeout setinterval


【解决方案1】:

前段时间我遇到了同样的问题,我做了一个小函数可以做到这一点: https://github.com/Atticweb/smart-interval/blob/master/smart-interval.js

它是这样工作的:

var timer = new timer();
timer.start(function(){
    //more magic here
}, 3000, true);

//change the interval
timer.set_interval(4000);

希望对你有帮助,祝你好运!

【讨论】:

    猜你喜欢
    • 2022-10-05
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多