我正在使用生成器函数
创建一个迭代器public class PersonalInformation extends Activity{
private SharedPreferences savedFields;
private Button saveButton;
private EditText editText1;
private EditText editText2;
// Add all your EditTexts...
// Upon creating your Activity, reload all the saved values.
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
saveButton = (Button) findViewById(R.id.your_save_button_id);
editText1 = (EditText) findViewById(R.id.your_edit_text_1_id);
editText2 = (EditText) findViewById(R.id.your_edit_text_2_id);
// Keep adding all your EditTexts the same way...
// "info" is just a tag name, use anything you like
savedFields = getSharedPreferences("info", MODE_PRIVATE);
// In case no value is already saved, use a Default Value
editText1.setText(savednotes.getString("editText1", "Default Value 1"));
editText2.setText(savednotes.getString("editText2", "Default Value 2"));
// Save the changes upon button click
saveButton.setOnClickListener(saveButtonListener);
}
public OnClickListener saveButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor preferencesEditor = savedFields.edit();
if(editText1.getText().length() > 0) // Not empty
preferencesEditor.putString("editText1", editText1.getText());
if(editText2.getText().length() > 0) // Not empty
preferencesEditor.putString("editText2", editText2.getText());
// You can make a function so you woudn't have to repeat the same code for each EditText
// At the end, save (commit) all the changes
preferencesEditor.commit();
}
}
};
}
我希望得到
function *func () {
yield setTimeout(()=>{
return 'ret1';
}, 1000);
yield 'ret2'
}
let result = func();
console.log(result.next());
console.log(result.next());
但我得到的是
Object {value: "ret1", done: false}
Object {value: "ret2", done: false}
这里有一个小提琴 https://jsfiddle.net/8t65kqjv/
答案 0 :(得分:0)
对setTimeout
的调用将返回已创建的超时的ID。这就是第一个对象的值为1
的原因,因为这是创建的超时的id。
您可以通过链接生成器并在生成器函数中添加日志来部分实现此功能,如下所示:
function getValue() {
setTimeout(function() {
result.next("ret1")
}, 1000);
}
function* func() {
yield "ret2";
var a = yield getValue();
console.log(a);
}
var result = func();
console.log(result.next());
console.log(result.next());
答案 1 :(得分:0)
也许试试这个?我不确定这是不是你的目标...
function *func () {
yield 'ret1'
yield 'ret2'
}
let result = func()
var tick = setInterval(()=>{
let now = result.next();
!now.done ? console.log(now) : clearInterval(tick)
}, 1000)