是否可以在jScript中使用多个匿名setTimeout函数执行以下操作?
import sys
def main(key, encoded):
if encoded == 0: #This should run if program was double-clicked (no arguments)
encoded = input("Paste encoded JavaScript: ")
key = input("Enter decode key: ")
def decode(key, encoded): #Decode the data
encoded = encoded.split(',') #Split each section delimiting by a colon
decoded = []
for x in encoded:
x = int(x) - int(key) #Subtract the key from the number in each section
decoded.append(chr(x)) #Change from ASCII decimal code to the ASCII character
decoded = ''.join(decoded) #Join back into a string
print(".")
print(".")
print(".")
print(".")
print("Encoded data:")
print(encoded)
print("Decode key:")
print(key)
print("Decoded data:")
print(decoded)
return 0
decode(key, encoded) #Jump into the decode function
return 0
if __name__ == "__main__":
try:
if len(sys.argv) > 1: #If length is greater than 1, then there were arguments added upon program execution
key = sys.argv[1] #The "key" should be the first argument
encoded = sys.argv[2] #The "encoded" data should follow
else:
key = 0 #If length is anything else, then set them to 0 and ask for the data later
encoded = 0
main(key, encoded) #Jump into main function and pass the key and encoded arguments
finally:
input("Press Enter to exit")
我曾希望这样的事可能有用:
var eventCounter;
// wait for eventCounter to be equal to 1
console.log('eventCounter is now equal to one');
// pause for eventCounter to be equal to 2
console.log('eventCounter is now equal to two');
// finally, wait for eventCounter to be equal to 3
console.log('eventCounter is now equal to three');
或者承诺是要走的路?
否则看起来我最终会遇到一些带有多个函数名的深度嵌入式嵌套执行循环,这是我希望避免的。
非常感谢你。答案 0 :(得分:0)
我在SO上发现了这个,但不幸的是我无法记住我在哪里找到它:
var waitValue = false;
var counter = 0;
(function tester() {
if ( waitValue ) {
console.log('finally true');
} else {
if ( counter > 1000 ) {
console.log('waited too long');
process.exit;
} else {
console.log('still waiting, counter = ' + counter++);
setTimeout(tester, 1000);
}
}
})();
// wait a few seconds and enter this into the console:
var waitValue = false;
另一个可能更好的主意是suggested here:
(function loop(counter) {
if ( waitValue ) {
console.log('waitValue is now true');
resolve('FIRST PROMISE');
} else if ( counter <= 0 ) { // dont wait forever.
reject('waited too long');
} else {
console.log('Count down: ' + counter);
setTimeout( loop.bind(null, counter-1), 3000);
}
})(counter); // initial value of count-down