简体   繁体   English

Javascript:将数字变量从函数传递到setInterval

[英]Javascript: Pass number variable from a function to setInterval

var items = [ 
  ["crunches", 30], 
  ["crunches", 45], 
  ["squats", 30], 
  ["squats", 45], 
  ["lunges", 30],
  ["lunges", 45], 
  ["wall sits", 30],
  ["wall sits", 45], 
  ["push ups", 30],
  ["push ups", 45], 
  ["leg ups", 30],
  ["leg ups", 45], 
  ["jumping jacks", 30],
  ["jumping jacks", 45], 
  ["sumo squat", 30],
  ["sumo squat", 45] 
];

I'm walking through this array, removing an element each time, but i want to take the number value from each item and make that the delay for a setInterval outside of the function. 我正在遍历此数组,每次都删除一个元素,但是我想从每个项目中获取数字值,并使setInterval的延迟超出函数范围。 Is this even possible? 这有可能吗?

Apologies for the poor JS, still learning a bit here. 为可怜的JS道歉,仍然在这里学习了一些。

Demo: https://jsfiddle.net/mattbtay/dcqdf8ng/1/ 演示: https : //jsfiddle.net/mattbtay/dcqdf8ng/1/

As I think you know, setInterval() has a constant duration between iterations so if you want a different delay between each iteration, then it makes more sense to use a repeated setTimeout() call each time where the next delay for setTimeout() is specifically set from the previous selection. 当我想你知道, setInterval()有迭代之间恒定的持续时间,所以如果你想每次迭代之间不同的延迟,那么它对使用重复更有意义setTimeout()每次调用其中的下一个延迟setTimeout()是从上一个选择中专门设置。 This will create the equivalent of a recurring timer, but with a varying interval. 这将创建一个等效的重复计时器,但间隔会有所变化。

Here's a general idea how you could do that. 这是一个一般性的想法,您将如何做到这一点。 Based on your jsFiddle, it looks like you want to randomly select an item from the array each time so that's how I've shown the implementation. 基于您的jsFiddle,看来您想每次都从数组中随机选择一个项目,因此这就是我展示实现的方式。 You didn't say what the units were meant to be on the time values. 您没有说时间值的单位是什么。 I assumed they were a number of seconds. 我以为他们是几秒钟。

function processArray(arr, processItem, done) {
    // make copy of initial array so we don't modify original data
    var data = arr.slice();
    function next() {
        if (data.length) {
            // get random index
            var index = Math.floor(Math.random() * data.length);
            var item = data[index];

            // remove the selected item from the array
            data.splice(index, 1);

            // now process the randomly selected item
            processItem(item[0], item[1]);

            // set timer for next item based on the time in this one
            setTimeout(next, item[1] * 1000);
        } else {
            done();
        }
    }
    next();
}

processArray(items, function(activity, time) {
      $('#activity').html(activity + ' ' + time);
}, function() {
      // this is called when all the activities are done
});

Here is an example of getting a random item from items, show in html, remove it, wait x milliseconds and then start again until no items are left. 这是一个从项目中获取随机项目的示例,以html显示,将其删除,等待x毫秒,然后再次开始直到没有剩余项目为止。 When none are left, it triggers the .then function from the promise. 当一无所有时,它将从promise触发.then函数。

 var items = [ ["crunches", 30], ["crunches", 45], ["squats", 30], ["squats", 45], ["lunges", 30], ["lunges", 45], ["wall sits", 30], ["wall sits", 45], ["push ups", 30], ["push ups", 45], ["leg ups", 30], ["leg ups", 45], ["jumping jacks", 30], ["jumping jacks", 45], ["sumo squat", 30], ["sumo squat", 45] ]; function removeItems(processItemRemoved) { return new Promise(function(resolve, reject) { function removeItem() { if (items.length) { // if there are still items to remove var randomIndex = Math.floor(Math.random() * items.length); // pick a random index between 0 and length-1 var item = items[randomIndex] items.splice(randomIndex, 1) // remove from array processItemRemoved(item) // process removed item setTimeout(removeItem, item[1]) // call function again after items[1] milliseconds } else { // if it has finished, run callback function resolve() } } removeItem() }) } removeItems(function(item) { $("#activity").html(item) }).then(function(){ console.log("all items finished") }) 
 #activity { text-align: center; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="container"> <div class="row"> <div class="col-md-12"> <div class="jumbotron"> <h1 id="activity"></h1> <button class="btn btn-primary" id="startBtn">Begin</button> </div> </div> </div> </div> 

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

相关问题 JavaScript:使用setInterval()将变量从一个函数传递到另一个函数? - JavaScript: Pass a variable from one function to another, using setInterval()? Javascript-如何从setInterval()函数获取变量? - Javascript - How do I get a variable from a setInterval() function? 在angularjs中将变量作为setInterval函数的参数传递 - pass an variable as an argument for an setInterval function in angularjs 如何将 React 状态变量传递给 setInterval 函数 - How to pass React state variable into setInterval function JavaScript SetInterval-通过函数指针而不是String - JavaScript SetInterval - Pass function pointer instead of String Javascript:setInterval()函数中的变量的计数增加5 - Javascript: Increment count by 5 for a variable inside a setInterval() function 使用函数更新由对象创建的对象中的Javascript变量setInterval - Update Javascript variable setInterval in an object created by function with setInterval()函数内部的Javascript变量未更改? - Javascript variable not changing inside setInterval() function? 将变量从php传递到javascript函数参数 - Pass variable from php to javascript function argument Javascript从函数返回变量并作为参数传递 - Javascript return variable from function and pass as parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM