2017-07-08 223 views
0

我按照example的要求實現了一個微調器。該代碼加載font在加載頁面時加載字體

我的問題是,按鈕調用ajax和ajax調用後加載的字體(儘管按鈕圖標更改首先由javascript請求)。 ajax和font都是從本地服務器調用的。這個服務器不是多任務的,當ajax被調用時,字體不會被加載,直到ajax調用完成。

有沒有什麼辦法強制瀏覽器加載頁面加載時的字體?

$('.btn').on('click', function() { 
 
    var $this = $(this); 
 
    $this.button('loading'); 
 
    setTimeout(function() { 
 
     $this.button('reset'); 
 
    }, 800000); 
 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/js/bootstrap.min.js"></script> 
 
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet"/> 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"/> 
 
<div style="margin:3em;"> 
 
<button type="button" class="btn btn-primary btn-lg " id="load" data-loading-text="<i class='fa fa-circle-o-notch fa-spin'></i> Processing Order">Submit Order</button> 
 
<br> 
 
    <br> 
 
<button type="button" class="btn btn-primary btn-lg" id="load" data-loading-text="<i class='fa fa-spinner fa-spin '></i> Processing Order">Submit Order</button> 
 
</div>

UPDATE

我的代碼具有這樣的結構:

$(button).button('loading'); 

$.post("/my_page", 
    JSON.stringify({ 
      param: "1234", 
        }), 
    function(data) 
    { 
     // .... 
     $(button).button('reset'); 
    } 
    ) 
    .fail(function(xhr, status, error) 
    { 
     // error handling 
     $(button).button('reset'); 
    }); 
+0

在您的示例中,在超時完成後,我看不到加載的字體。在超時之後你想讓你的按鈕使用不同的字體嗎?請澄清一下。 – threeFatCat

+0

@threeFatCat,我想要的是字體加載時立即加載字體。所以,在按下按鈕之前,字體被加載。我無法在這裏複製C++服務器。請參閱更新。 – ar2015

回答

0

,那麼這將做.. 一旦頁面加載和腳本已準備就緒。 。做一個ajax調用來加載你的字體..

$(document).ready(function(){ 
    //your ajax.. call here to load the font. 
}); 
+0

ajax不加載字體。 ajax調用一個需要幾秒鐘響應的頁面。字體是由我的控制引導腳本加載的。 – ar2015

+0

你可以試試[這個](https://stackoverflow.com/questions/4383226/using-jquery-to-know-when-font-face-fonts-areloaded) – threeFatCat