第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何使用用戶(hù)輸入更改 setInterval 中的時(shí)間?

如何使用用戶(hù)輸入更改 setInterval 中的時(shí)間?

慕田峪9158850 2021-11-25 16:15:40
我想知道一種更改 setInterval 時(shí)間的方法,以便我的圖像以該速度在屏幕上移動(dòng)。例如,如果我輸入 500 毫秒,它會(huì)在我單擊按鈕時(shí)將時(shí)間間隔從 250 更改為 500。這是我到目前為止想到的。 var x; var y; var timing = 1000; function window_onLoad() {     x = 0;     y = 100;     window.setInterval("MoveBall()", timing);     picBall.style.top = y + "px"; }  function MoveBall() {     x = x + 5;     if (x < document.body.clientWidth - 91) {        picBall.style.left = x + "px";    }}function btnReset_OnClick() {    x = 0;}function btnSpeed_OnClick() {    timing = parseInt(txtSpeed.value);}window_onLoad()<img id="picBall" src="Face.jpg" style="position: absolute;"/><input id="btnReset" type="button" value="Reset position"       onclick="btnReset_OnClick()"/><input id="txtSpeed" type="text"/><input id="btnSpeed" type="button" value="Change Speed"   oclick="btnSpeed_onClick()"/>
查看完整描述

3 回答

?
繁星淼淼

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊

我建議不要將移動(dòng)速度與幀率(您的 setInterval 速度)混合。您可以擁有固定的幀率和可變的速度。例如


var speed = 1, timer, x,y;



 function window_onLoad() {

     x = 0;

     y = 100;

     window.setInterval("MoveBall()", 100); // 10 frames per second

     picBall.style.top = y + "px";

 } 

 function MoveBall() {

     x = x + speed;

     if (x < document.body.clientWidth - 91) {

        picBall.style.left = x + "px";

    }

}

function btnReset_OnClick() {

    x = 0;

}

function btnSpeed_OnClick() {

    /*

       speed = 200 will move tbe ball by 20px per sec

       speed = 100 will move the ball by 10px per sec

       speed = 50 will move the ball by 5px per sec

     */

    speed = parseInt(txtSpeed.value)/100;

}


查看完整回答
反對(duì) 回復(fù) 2021-11-25
?
慕后森

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

您的主要問(wèn)題是您需要清除先前的間隔并創(chuàng)建一個(gè)新的間隔。但是,我建議將創(chuàng)建間隔的代碼移動(dòng)到另一個(gè)像這樣的函數(shù)中......


function window_onLoad() {

  x = 0;

  y = 100;

  createInterval(timing);

  picBall.style.top = y + "px";


var intervalId = 0;


// this will destroy any existing interval and create a new one

function createInterval(interval) {

  clearInterval(intervalId);

  intervalId = setInterval(MoveBall, interval);

}


function btnSpeed_OnClick() {

  timing = parseInt(txtSpeed.value);

  createInterval(timing);

}


查看完整回答
反對(duì) 回復(fù) 2021-11-25
?
慕雪6442864

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊

您必須保存對(duì)間隔的引用,然后,每次要更改速度時(shí),您都必須清除前一個(gè)間隔,clearInterval然后應(yīng)用新的間隔,如下所示:


 var x;

 var y;

 var timing = 1000;

 var interval;


 function window_onLoad() {

     x = 0;

     y = 100;

     applyInterval();

     picBall.style.top = y + "px";

 } 


function applyInterval() {

  if (interval) {

    console.log('Clearing previous interval');

    clearInterval(interval);

  }

  console.log('Applying new interval');

  interval = window.setInterval("MoveBall()", timing);

}


function MoveBall() {

     x = x + 5;

     if (x < document.body.clientWidth - 91) {

        picBall.style.left = x + "px";

    }

}

function btnReset_OnClick() {

    x = 0;

}

function btnSpeed_OnClick() {

    timing = parseInt(txtSpeed.value);

    applyInterval();

}


window_onLoad()

<img id="picBall" src="https://i.stack.imgur.com/vnucx.png?s=64&g=1" style="position: absolute;" width="25" height="25"/>

<input id="btnReset" type="button" value="Reset position"

       onclick="btnReset_OnClick()"/>

<input id="txtSpeed" type="text"/>

<input id="btnSpeed" type="button" value="Change Speed"

   onclick="btnSpeed_OnClick()"/>


查看完整回答
反對(duì) 回復(fù) 2021-11-25
  • 3 回答
  • 0 關(guān)注
  • 219 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)