简体   繁体   English

我可以通过将其值设置为 function 来更新变量吗?它使用 setInterval 每 10 毫秒获取一个外部值?

[英]can I update a variable by making its value an function which gets an external value every 10 milliseconds with setInterval?

Can I update a variable by making its value a function that gets an external value every 10 milliseconds with setInterval?我可以通过将其值设为 function 并使用 setInterval 每 10 毫秒获取一个外部值来更新变量吗? I'm a beginner so be kind.我是初学者,所以请客气。

var scrollTop = setInterval(function() {
var scrollTop = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
}, 10);

These are some of the observations and suggestions to improve your code,hope this will help you.这些是改进代码的一些观察和建议,希望对您有所帮助。

  • Set Interval will return an interval ID which means your var scrollTop will have an id value like(0,1,2,4) instead of an offset value. Set Interval 将返回一个间隔 ID,这意味着您的 var scrollTop 将具有类似(0,1,2,4)的 id 值而不是偏移值。

  • set Interval will take a callback function means just define your function outside of the interval and use that.设置间隔将采用回调 function 意味着只需在间隔之外定义您的 function 并使用它。 it will make your code cleaner and easy to debug.它将使您的代码更清晰且易于调试。

also you are beginner so you can read about this terms to understand it better callback functions, setInterval, arrow function(()=>)你也是初学者所以你可以阅读这个术语以更好地理解回调函数,setInterval,箭头函数(()=>)

function offSetValue(){
     var scrollTop = (window.pageYOffset !== undefined) ? window.pageYOffset : 
    (document.documentElement || document.body.parentNode || 
     document.body).scrollTop;
}

setInterval(() => offSetValue(), 10);

暂无
暂无

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

相关问题 在SetInterval函数外部更新变量值 - Update Variable Value Outside SetInterval Function 我可以使用哪种技术将对象的值加载到表单字段中,然后更新该值以使其可用于我的$ scope变量 - which technique can I use to load an object's value in a form field and then update that value making it available to my $scope variable 单击按钮时如何在setinterval函数中更新变量值 - How to update the variable value in setinterval function on button click setInterval可以在变量中存储一个值 - Can setInterval store a value in a variable 如何在Jquery中访问变量并在另一个外部.js文件中使用它的值? 变量为$ generationP - How can I access a variable in Jquery and use its value in another external .js file ? The variable is $generatedP 如何更新由函数(和函数内部)更改的数组,以便其更新后的值可以在新函数中使用? - How to update an array which is changed by (and inside) a function so that its updated value(s) can be used in a new function? 如何从setInterval函数返回值? - How can I return value from setInterval function? 如何将参数传递给setInterval毫秒值? - how to pass parameter to setInterval milliseconds value? 我正在制作一个每 1000 毫秒减少一次计时器的函数,那么如果我重新启动,有没有办法减少剩余的毫秒数? - I'm making a function to reduce the timer every 1000 milliseconds, so is there a way to reduce the remaining milliseconds if I restart? setinterval函数,使用先前/旧值作为变量 - setinterval function using previous/ old value for a variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM