2017-01-04 67 views
-1

我如何在Swift中循環睡眠?在java中保存代碼執行(在循環中)

int userI = 0; 
for (userI = 0; userI < users.size()/10; userI++) { 
    // do something 
    // Wait for 5 seconds. 
} 

我已經熟悉了這一點:

final Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 
    @Override 
    public void run() { 
    //Do something after 100ms 
    } 
}, 100); 

但我不這麼認爲其會在這種情況下幫助,因爲我不想火的功能我只想等待後或者保持到循環完成。

+1

內能請解釋一下多一點 –

+1

耶確保它是@HotAppleCyber​​如果'Java的== swift' –

+0

哎呀:-) 謝謝。漫長的一天 – clearlight

回答

2

使用Thread.sleep()方法時要幾分鐘

睡覺
int userI = 0; //users is an array 
int sleepSeconds =5000; 
for (userI = 0; userI < users.size()/10; userI++) { 

/// do something in this loop 
//// and after its done i wanna wait or hold for 5 seconds until loop again starts 

Thread.sleep(sleepSeconds); 
} 
1

您可以使用

Thread.sleep(5000); 

,如果你想停止和啓動了一些單獨的線程,你也可以用「等待」

2

我會編輯你的邏輯就像.....

int userI = 0; //users is an array 
    for (userI = 0; userI < users.size()/10; userI++) { 

    /// do something in this loop 
    //// and after its done i wanna wait or hold for 5 seconds until loop again starts 
} 
final Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 
    @Override 
    public void run() { 
    // Don't do anything 
    } 
}, 5000); 

//Do you login here 

final Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 
@Override 
public void run() { 
    int userI = 0; //users is an array 
     for (userI = 0; userI < users.size()/10; userI++) { 

     /// do something in this loop 
     //// and after its done i wanna wait or hold for 5 seconds until loop again starts 
    }  
    } 
}, 5000); 

// your logic after wait... 

,或者你可以嘗試Thread.sleep(4000);你的循環這樣

int userI = 0; //users is an array 
     for (userI = 0; userI < users.size()/10; userI++) { 

     /// do something in this loop 
     Thread.sleep(4000); //4 sec 
    }