2013-03-26 70 views
0

你好我找到一個特定的工作模式;模式如果失敗1嘗試2如果失敗嘗試3爲nodejs

可以說IM尋找一個標題在頁面的DOM

如果發現標題,然後把它放到VAR標題 如果VAR標題仍然是空的,然後嘗試下一個功能 如果VAR標題仍然是空的,然後試了下功能

是有那麼一個更好的辦法

// Find Title 
output.title = $('title').text(); 

if (null(output.title)) { 
    output.title = second try 
}; 

if (null(output.title)) { 
    output.title = 3rd try 
}; 

etc ? 
+0

你可以利用的setInterval的'()'和 – Gntem 2013-03-26 10:12:27

回答

1

我的版本使得它更可擴展性和邏輯性。 使用數組和while循環(使用異步模塊):

var functions = [function1, function2, function3] 
var i = 0 
var output.title // to deal with scope issue of output.title only being defined inside whilst. Could have put output.title as argument for callback 
async.whilst(
    function() { return i < functions.length && !output.title }, 
    function (callback) { 
      output.title = functions[i] 
      i++ 
      callback() 
}, function() { 
    if (output.title) { 
     //have title 
    } 
    else { 
     // no title was found 
    } 
}) 
+0

感謝檢查每'X'毫秒更改async.js同時是尋找什麼即時通訊對於。從來沒有聽說過。 – user1780413 2013-03-26 10:51:37

+0

沒問題,這是一個很棒的小模塊,可以做你想要的一切。 – Niall 2013-03-26 11:07:08