2012-08-22 9 views
7

में मैं एकाधिक सेटटाइमआउट को जावास्क्रिप्ट लूप के भीतर कॉल कर रहा हूं। देरी वर्तमान में प्रत्येक प्रतिरक्षा पर 200ms तक बढ़ने के लिए सेट है जिससे 'self.turnpages()' फ़ंक्शन हर 200ms में आग लगती है।सेटटाइमआउट देरी में आसान लगाना, लूप

हालांकि मैं इन परिवर्तनीय देरी में कुछ प्रकार की आसानता लागू करना चाहता हूं ताकि लूप पिछले कुछ पुनरावृत्तियों तक पहुंचने लगे, क्योंकि फंक्शन फायरिंग धीमा होने के कारण देरी और अलग हो जाती है।

var self = this;  
var time = 0; 

for(var i = hide, len = diff; i < len; i++) { 
        (function(s){ 
          setTimeout(function(){      
             self.turnPages(s);       
          }, time); 
         })(i);         
      time = (time+200); 
} 

मैं इस बात से पूरी तरह से हानि में हूं कि इस से कैसे शुरुआत करें।

उम्मीद है कि कोई मदद कर सकता है।

+2

के बजाय 200 एक निरंतर किया जा रहा है, यह "मैं" के एक समारोह होना चाहिए। – Pointy

+0

@ पॉइंटी - दरअसल, मुझे नहीं पता कि मुझे आवश्यक चीज़ों को प्राप्त करने के लिए गणित के साथ कहां से शुरुआत करना है। – gordyr

+1

वैसे यह इस बात पर निर्भर करता है कि आप क्या अनुमान लगाते हुए दिखने वाले वक्र को देखना चाहते हैं। – Pointy

उत्तर

9

यह रॉबर्ट पेननर के आसान समीकरणों के लिए नौकरी की तरह लगता है! आप मूल एक्शनस्क्रिप्ट 2.0 संस्करण here (केवल जावास्क्रिप्ट पर बंदरगाह पर पैरामीटर पर मजबूत-टाइपिंग को हटा सकते हैं) डाउनलोड कर सकते हैं और here पैरामीटर का एक अच्छा स्पष्टीकरण है।

कुछ निम्नलिखित की तरह आप क्या चाहते हैं (fiddle) करना होगा:

var time = 0; 
var diff = 30; 

var minTime = 0; 
var maxTime = 1000; 

// http://upshots.org/actionscript/jsas-understanding-easing 
/* 
    @t is the current time (or position) of the tween. This can be seconds or frames, steps, seconds, ms, whatever – as long as the unit is the same as is used for the total time [3]. 
    @b is the beginning value of the property. 
    @c is the change between the beginning and destination value of the property. 
    @d is the total time of the tween. 
*/ 
function easeInOutQuad(t, b, c, d) { 
    if ((t /= d/2) < 1) return c/2 * t * t + b; 
    return -c/2 * ((--t) * (t - 2) - 1) + b; 
} 

function easeOutQuad(t, b, c, d) { 
    return -c * (t /= d) * (t - 2) + b; 
} 

function easeInQuad(t, b, c, d) { 
    return c * (t /= d) * t + b; 
} 

for (var i = 0, len = diff; i <= len; i++) { 
    (function(s) { 
    setTimeout(function() { 
     //self.turnPages(s);       
     console.log("Page " + s + " turned"); 
    }, time); 
    })(i); 

    time = easeInOutQuad(i, minTime, maxTime, diff); 
    console.log(time); 
} 
+0

बिल्कुल सही। धन्यवाद – gordyr

+0

कोई समस्या नहीं gordyr, खुश आसान! –

संबंधित मुद्दे